我正在尝试添加相关的
"-I"path_to_your_SDL_include_directory""
,如几篇类似帖子(例如 this one)中所述。我尝试了三种方法;将其添加到tasks.json、Makefile和c_cpp_properties.json中。
我的文件结构如下。我的 main.cpp 位于
MyProject/src
中。我已将SDL的include文件夹的所有内容复制到MyProject/lib/SDL2_lib/include
并将lib文件夹复制到MyProject/lib/SDL2_lib/lib
。 SDL2.dll
住在MyProject/lib/SDL2_lib
。
以下是视觉摘要以及我的代码。
main.cpp
#include <iostream>
#include <SDL.h>
const int WIDTH = 800, HEIGHT = 600;
int main( int argc, char *argv[] )
{
SDL_Init( SDL_INIT_EVERYTHING );
SDL_Window *window = SDL_CreateWindow( "Hello SDL WORLD", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI );
if ( NULL == window )
{
std::cout << "Could not create window: " << SDL_GetError( ) << std::endl;
return 1;
}
SDL_Event windowEvent;
while ( true )
{
if ( SDL_PollEvent( &windowEvent ) )
{
if ( SDL_QUIT == windowEvent.type )
{ break; }
}
}
SDL_DestroyWindow( window );
SDL_Quit( );
return EXIT_SUCCESS;
}
生成文件
all:
g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -o Main src/main.cpp
任务.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-I lib/SDL2_lib/include",
"-L lib/SDL2_lib/lib",
"-lmingw32",
"-lSDL2main",
"-lSDL2",
"-o",
"${workspaceFolder}/bin\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
c_cpp_properties.json
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/lib/SDL2_lib/include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"compilerPath": "C:\\MinGW\\bin\\g++.exe",
"cStandard": "gnu11",
"cppStandard": "c++14",
"intelliSenseMode": "windows-gcc-x86",
"configurationProvider": "ms-vscode.makefile-tools",
"compilerArgs": [
"-I lib/SDL2_lib/include",
"-L lib/SDL2_lib/lib",
"-lmingw32",
"-lSDL2main",
"-lSDL2"
]
}
],
"version": 4
}
如有任何帮助,我们将不胜感激!
编辑:我还应该补充一点,添加随机文件名而不是
SDL.h
强调了整个包含语句而不仅仅是结尾。很明显,VSCode 确实知道它存在,只是没有将其添加到程序中,这就是我的猜测
Edit2:从 powershell 运行
make
会出现以下错误;
很明显你有两个问题。
让我们先解决 Makefile 的问题:
你的 Makefile 中有一个拼写错误,它写的是
SDL2_lib/libr
而不是 SDL2_lib/lib
。
修复此问题后,您必须添加要链接的库。从技术上讲,您只需要
libSDL2.la
(假设动态链接),因为您已经编写了自己的 main()
函数(因此您不需要 SDL2main
。
因此,Makefile 中的命令行应该如下所示(请注意我如何将项目的文件放在库之前以保证正确加载符号,这在使用中间文件时变得更加重要):
g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
-lmingw32 -lSDL2main -lSDL2 -mwindows \
-o Main
如果这不起作用,请提供编译器输出(作为文本,而不是图像),但在尝试此操作之前不要提供:
选项一:指定整个库的文件名:
g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
-lmingw32 -lSDL2main.la -llibSDL2.la -mwindows \
-o Main
选项二:静态链接(您不需要 DLL)
g++ -Ilib/SDL2_lib/include -Llib/SDL2_lib/lib src/main.cpp \
-lmingw32 lib/SDL2_lib/lib/libSDL2main.a lib/SDL2_lib/lib/libSDL2.a -mwindows \
-o Main
关于 VSCode:
我不认为你的配置有什么问题。您可能想尝试切换到反斜杠(无论如何,这不会产生任何影响)或(是的,确实如此)重新加载 VSCode。
安装工具和库
brew install gcc
brew install sdl2
brew install sdl2_image
创建项目结构
mkdir -p game_app/{include,lib,src,build/debug}
cd game_app
cat << src/main.c << _EOF
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
typedef struct App
{
SDL_Window *window;
SDL_Renderer *renderer;
} App;
int main(void)
{
printf("Hello, world!\n");
return EXIT_SUCCESS;
}
_EOF
code .
我的
Makefile
CC = /opt/homebrew/bin/gcc-13
BUILD_DIR = build/debug
SRC_DIR = src
SRC_FILES = $(wildcard $(SRC_DIR)/*.c)
OBJ_NAME = shooter
INCLUDE_PATHS = -I/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13 -Iinclude/SDL2 -Iinclude/SDL2_image
LIBRARY_PATHS = -L/opt/homebrew/Cellar/gcc/13.2.0/lib/gcc/13 -Llib/SDL2 -Llib/SDL2_image
COMPILER_FLAGS = -std=c17 -Wall -Wextra -Werror -g
LINKER_FLAGS = -lSDL2 -lSDL2_image
all: $(SRC_FILES)
$(CC) $(COMPILER_FLAGS) $(LINKER_FLAGS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(SRC_FILES) -o $(BUILD_DIR)/$(OBJ_NAME)
clean:
rm -rf $(BUILD_DIR)/*
我的
vscode
配置
.vscode/settings.json
文件
{
"C_Cpp.intelliSenseEngine": "Tag Parser",
"C_Cpp.errorSquiggles": "disabled",
"C_Cpp.enhancedColorization": "enabled",
}
.vscode/c_cpp_properties.json
文件
{
"configurations": [
{
"name": "Mac",
"compilerPath": "/opt/homebrew/bin/gcc-13",
"intelliSenseMode": "macos-gcc-arm64",
"includePath": [
"${workspaceFolder}/**",
"/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13",
"${workspaceFolder}/include",
"${workspaceFolder}/lib"
],
"defines": [],
"macFrameworkPath": [
"${workspaceFolder}/**",
"/System/Library/Frameworks",
"/Library/Frameworks"
],
"cStandard": "c17",
"cppStandard": "c++17",
"configurationProvider": "ms-vscode.makefile-tools",
"browse": {
"path": [
"/opt/homebrew/Cellar/gcc/13.2.0/include/c++/13",
"${workspaceFolder}/include",
"${workspaceFolder}/lib"
]
}
}
],
"version": 4
}
.vscode/launch.json
文件
{
"version": "2.0.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/debug/shooter",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb",
"preLaunchTask": "build"
}
]
}
.vscode/tasks.json
文件
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "build",
"command": "make",
"args": [],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Build our program using make"
}
],
}
我没有使用过 Windows 和 VSCode,但我想你可以尝试一下
g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -lsdl -o Main src/main.cpp
或
g++ -I lib/SDL2_lib/include -Llib/SDL2_lib/lib -lsdl2 -o Main src/main.cpp
标志之间不应有空格 IE “-I lib/SDL2_lib/include”应为“-Ilib/SDL2_lib/include” “-L lib/SDL2_lib/lib”应该是“-Llib/SDL2_lib/lib”