如何将库(SDL2)链接到C++;使用VSCode和CMake(Windows)的项目 这里是这样的:我想为项目使用SDL,但是从头开始创建C++项目是新的,没有办法将这个库链接到我的项目。这是因为我对CMake以及tasks.json和CMakeLists.txt文件缺乏了解
在谷歌搜索了几个小时后,我现在拥有了什么 main.cpp如何将库(SDL2)链接到C++;使用VSCode和CMake(Windows)的项目 这里是这样的:我想为项目使用SDL,但是从头开始创建C++项目是新的,没有办法将这个库链接到我的项目。这是因为我对CMake以及tasks.json和CMakeLists.txt文件缺乏了解,c++,visual-studio-code,cmake,C++,Visual Studio Code,Cmake,在谷歌搜索了几个小时后,我现在拥有了什么 main.cpp #include <iostream> #include "SDL/inc/SDL.h" int main() { const int image_width = 200; const int image_height = 100; std::cout << "P3\n" << image_width << ' ' << image_height &
#include <iostream>
#include "SDL/inc/SDL.h"
int main() {
const int image_width = 200;
const int image_height = 100;
std::cout << "P3\n" << image_width << ' ' << image_height << "\n255\n";
for (int j = image_height-1; j >= 0; --j) {
std::cerr << "\rScanlines remaining: " << j << ' ' << std::flush;
for (int i = 0; i < image_width; ++i) {
auto r = double(i) / image_width;
auto g = double(j) / image_height;
auto b = 0.2;
int ir = static_cast<int>(255.999 * r);
int ig = static_cast<int>(255.999 * g);
int ib = static_cast<int>(255.999 * b);
std::cout << ir << ' ' << ig << ' ' << ib << '\n';
}
}
std::cerr << "\nDone.\n";
}
tasks.json:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"SuppressTaskName": true,
"type": "shell",
"options": {
"cwd": "${workspaceRoot}/build"
},
"tasks": [
{
"label": "cmake",
"command": ["cmake -G 'Unix Makefiles' -DCMAKE_BUILD_TYPE=Debug .."]
},
{
"label": "make",
"command": ["make -j 8tasks.json"],
"group": {
"kind": "build",
"isDefault": true,
}
}
]
}
c_cpp_properties.json:
{
"configurations": [
{
"name": "Win32",
"includePath": [
"${workspaceFolder}/**",
"${workspaceFolder}/SDL/inc",
"${workspaceFolder}/SDL/lib/x64"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe",
"cStandard": "c11",
"cppStandard": "c++17",
"intelliSenseMode": "msvc-x64"
}
],
"version": 4
}
最后是SDL文件夹中的CMakeList,该文件夹位于项目根文件夹中,其中.cpp为:
message("-- Linking SDL")
add_library(SDL2 SDL2.dll)
set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE CXX)
(代码在库文件之前编译得很好)
编译器是这样说的:
make[1]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
[ 33%] Built target SDL2
make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
[ 66%] Linking CXX executable ArchTracer.exe
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against
undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ArchTracer.dir/build.make:85: ArchTracer.exe] Error 1
make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/ArchTracer.dir/all] Error 2
make[1]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
make: *** [Makefile:84: all] Error 2
The terminal process terminated with exit code: 1
我想了解如何告诉cmake include文件在哪里,lib在哪里。
我已经告诉VSCode在哪里可以找到这些
有人能帮忙吗
非常感谢:)欢迎来到Stack Overflow,这是一个很好的、有文档记录的问题!
add_library()
命令接受源文件作为参数,而不是已经生成的DLL文件。请参阅这些,以了解在CMake项目中包含外部库的方法。这将有助于进行必要的CMake更改:关键部分是第一个CMakeLists.txt
欢迎使用堆栈溢出,这是一个很好的、有充分文档证明的问题!add_library()
命令接受源文件作为参数,而不是已经生成的DLL文件。请参阅以下内容,了解在CMake项目中包含外部库的方法。这将有助于进行必要的CMake更改:关键部分是第一个CMakeLists.txt
make[1]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
[ 33%] Built target SDL2
make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
make[2]: Entering directory '/cygdrive/d/Development/ArchTracer/build'
[ 66%] Linking CXX executable ArchTracer.exe
/usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../x86_64-pc-cygwin/bin/ld: /usr/lib/gcc/x86_64-pc-cygwin/9.3.0/../../../../lib/libcygwin.a(libcmain.o): in function `main':
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37: undefined reference to `WinMain'
/usr/src/debug/cygwin-3.1.4-1/winsup/cygwin/lib/libcmain.c:37:(.text.startup+0x7f): relocation truncated to fit: R_X86_64_PC32 against
undefined symbol `WinMain'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ArchTracer.dir/build.make:85: ArchTracer.exe] Error 1
make[2]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/ArchTracer.dir/all] Error 2
make[1]: Leaving directory '/cygdrive/d/Development/ArchTracer/build'
make: *** [Makefile:84: all] Error 2
The terminal process terminated with exit code: 1