我需要更改Vs代码中的某些内容吗?
这些是问题:
Starting build...
/usr/bin/g++ -fdiagnostics-color=always -g -o /home/kali/path/snakeMain /home/kali/path/*.cpp -lncurses
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:9: multiple definition of `gameOver'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:9: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:14: multiple definition of `headX'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:14: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:14: multiple definition of `headY'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:14: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:17: multiple definition of `tailX'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:17: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:17: multiple definition of `tailY'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:17: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:18: multiple definition of `tailLength'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:18: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:21: multiple definition of `current_dir'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:21: first defined here
/usr/bin/ld: /tmp/ccCKG1t0.o:/home/kali/path/Snake.h:23: multiple definition of `key'; /tmp/ccJxCKEW.o:/home/kali/path/Snake.h:23: first defined here
collect2: error: ld returned 1 exit status
Build finished with error(s).
* The terminal process terminated with exit code: -1.
* Terminal will be reused by tasks, press any key to close it.
我在Linux中使用最新的Vs代码。我今天又安装了一次。我认为问题在于链接错误。
这是我的Snake.cpp:
#include <iostream>
#include <ncurses.h>
#include <unistd.h>
#include "Snake.h"
// Setup
void initTerminal() {
}
void initSnake() {
}
// Draw
void drawPixel(int y, int x) {
}
void drawBorder() {
// PRINT MAP
}
void drawSnake() {
}
// Logic
bool collidesWithBorder() {
}
bool collidesWithSelf() {
}
void moveSnake() {
}
bool handleKey(int key) {
}
我的snakeMain.cpp:
#include <iostream>
#include <ncurses.h>
#include <unistd.h>
#include "Snake.h"
int main() {
//-----here is only code and no #includes------/
return 0;
}
我的蛇
// Copyright 2022
// Author: Darky
#ifndef SNAKE_H_
#define SNAKE_H_
//Golabal var
// Used for the Loop
bool gameOver;
//field Dimensions
const int f_width = 40;
const int f_height = 40;
// Coordinates for the Head of Snake
int headX, headY;
// Coordinates for the Tail, max Length (if we continue develop)
// inital tailLenght (actual length is in drawSnake())
int tailX[25], tailY[25];
int tailLength = 0;
// Direction param
enum direction { STOP = 0, LEFT, RIGHT, UP, DOWN};
direction current_dir;
// input of the User
int key;
// Functions
// Initialize the Game
void initTerminal();
void initSnake();
// Draw the Game
// Draw a Singel Pixel into Gamefield
void drawPixel(int y, int x);
// Draws the Gamefield
void drawBorder();
// Draw Snake with tail
void drawSnake();
// Logic of the Game
// Check if hit the border
bool collidesWithBorder();
// Check if collides with Snaketail
bool collidesWithSelf();
// set the direction of the snake
void moveSnake();
// pick input and transfer to direction
bool handleKey(int key);
#endif // SNAKE_H_
my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++ build active file",
"command": "/usr/bin/g++",
"args": [
"-fdiagnostics-color=always",
"-g",
"-o",
"${fileDirname}/${fileBasenameNoExtension}",
"${workspaceFolder}/*.cpp",
"-lncurses"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
{
"files.associations": {
"iostream": "cpp"
}
}
同样,如果我在控制台中启动游戏并用makefile编译它,那么游戏就可以运行了。但是在Vs代码中,我遇到了如上所述的问题。
谢谢大家抽出时间。