Here is the thing: I want to use SDL for a project, but being new to creating c++ projects from scratch, there is no way to link this library to my project. This is caused by my lack of knowledge about CMake and the tasks.json and CMakeLists.txt files.
What I have right now, after several hours of googling
The 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 << "\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";
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(ArchTracer)
set(SOURCE main.cpp)
include_directories("${PROJECT_SOURCE_DIR}/SDL")
add_subdirectory(SDL)
add_executable(${PROJECT_NAME} ${SOURCE})
target_link_libraries(${PROJECT_NAME} SDL2)
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,
}
}
]
}
The 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
}
and finally the CMakeList INSIDE the SDL folder, which is in the project root folder, where the .cpp is:
message("-- Linking SDL")
add_library(SDL2 SDL2.dll)
set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE CXX)
(The code was compiling fine before that library stuff)
This is what the compiler is saying:
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
I would like to understand how to tell cmake where are the include files and where is the lib. I already told VSCode where to find these.
Does someone can help with that ?
Thank you very much :)