How to properly include SDL2 in a c++ cmake project? [duplicate] - c++

This question already has answers here:
Using SDL2 with CMake
(12 answers)
Cmake cannot find library using "link_directories"
(4 answers)
Closed 10 months ago.
I'm using Visual Studio 2019 to make a small c++ game engine. I'm trying to link SDL2 with my project, and I got it to load the header files, but when I try to build it, I get an error. I'm using CMake btw.
LNK1104 cannot open file 'SDL2main.lib'
My project structure is as follows.
MyGame
-engine
-game
-sdl2
-include
-lib
Here is game/CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
add_executable (game
main.cpp
)
include_directories(${CMAKE_SOURCE_DIR}/engine)
target_link_libraries(game engine)
Here is my engine/CMakeLists.txt:
cmake_minimum_required (VERSION 3.8)
add_library(engine engine.h app.cpp app.h)
set(SDL2_INCLUDE_DIR ${CMAKE_SOURCE_DIR}/sdl2/include)
set(SDL2_LIB_DIR ${CMAKE_SOURCE_DIR}/sdl2/lib/x86)
include_directories(${SDL2_INCLUDE_DIR})
link_directories(${SDL2_LIB_DIR})
target_link_libraries(engine SDL2main SDL2)
Edit: I found a solution: I moved link_directories to the project’s global CMakeList and added a command to copy the SDL2.dll to the output directory.

Related

Link restbed with Cmake [duplicate]

This question already has answers here:
How to add "-l" (ell) compiler flag in CMake
(2 answers)
Closed 2 years ago.
In trying to link restbed with CMake I get the usual undefined function error. However, trying the exact same code linked with g++ test.cpp -o test -lrestbed works fine.
Furthermore, when I first implemented the CMakeLists.txt it also worked fine and as I added to the project it started facing issues. Now even a single restbed function is not defined.
My restbed includes are located at /usr/local/include and the shared objects to link at /usr/local/lib. Pretty standard locations.
CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(vcar-server)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -lrestbed")
file(GLOB CXX_EXEC "src/*.cpp")
add_subdirectory(vcar-embedded)
add_executable(vcar-server ${CXX_EXEC})
target_link_libraries(vcar-server vcar)
Do not use CMAKE_CXX_FLAGS, or rather use it as a "last resort" when configuring the build system as a user. Prefer to use object model in place of global variables. The modern cmake way would most probably be something along:
cmake_minimum_required(VERSION 3.11)
project(vcar-server)
add_subdirectory(vcar-embedded)
find_package(Threads REQUIRED)
add_executable(vcar-server ${cxx_exec})
target_link_libraries(vcar-server PUBLIC vcar restbed Threads::Threads)
set_target_properties(vcar-server PUBLIC CXX_STANDARD 20)

CMake Linking Instructions [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 3 years ago.
I am trying to link a shared object on my executable with an executable in my cmake project.
my CMakeLists.txt
cmake_minimum_required (VERSION 2.8)
project (proto_app)
add_executable( helloDemo hello.cpp )
add_executable( faissDemo 1-Flat.cpp )
My executable faissDemo doesn't compile like this because the shared object is not linked. The shared object is located in /usr/local/lib/libfaiss.so. My question is how should I link to the shared object in CMake?
I can compile 1-Flat.cpp successfully from the command line like:
$ g++ 1-Flat.cpp -L /usr/local/lib/ -lfaiss
You need to add the dir to the link_directories and add the lib as target_link_libraries:
cmake_minimum_required (VERSION 2.8)
project (proto_app)
link_directories(/usr/local/lib)
add_executable( helloDemo hello.cpp )
add_executable( faissDemo 1-Flat.cpp )
target_link_libraries(faissDemo faiss)

Cmake error: Cannot specify link libraries for target [duplicate]

This question already has an answer here:
Build project with "experimental/filesystem" using cmake
(1 answer)
Closed 3 years ago.
I am trying to build a simple CMake project but I'm having problems understanding CMake or at least the error I get.
The project i made is divided in a couple directories.
The main one has this cmake:
cmake_minimum_required(VERSION 3.7.2)
project(Raytracer)
set(CMAKE_CXX_STANDARD 14)
add_subdirectory(src)
set(COMMON_INCLUDES ${PROJECT_SOURCE_DIR}/inc)
From there my project splits in 2 folders src and inc.
and in src i have the following cmake with the idea to global all subfolders:
FILE(GLOB sub-dirs RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} *)
FOREACH(dir ${sub-dirs})
IF(IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/${dir})
ADD_SUBDIRECTORY(${dir})
ENDIF()
ENDFOREACH()
add_executable(raytracer main.cpp)
I also add the executable there which is in this main src folder.
From there on i want to be able to make sub folders adding with their own Cmake files and linking those files to my executable.
I have the following cmake:
set(OBJECTS
asdf.cpp
)
add_library(obj_files ${OBJECTS} ${COMMON_FILES})
target_link_libraries(raytracer obj_files)
but when I try to build I get the following error:
Cannot specify link libraries for target "raytracer" which is not built by this project.
Basically in Cmake file or toolchain.cmake the order of commands are important!
target_link_libraries() or target_include_directories() have to be always after add_executable()

How to link shared library *dll with CMake in Windows [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 12 months ago.
I have 2 files: library.dll and library.h with some code that I need in my own project. I'm working on Windows with Clion where I should config this with CMake.
I tried this way:
cmake_minimum_required(VERSION 3.6)
project(test2)
set(CMAKE_CXX_STANDARD 11)
link_directories(C:\\Users\\Johny\\CLionProjects\\test2)
set(SOURCE_FILES main.cpp)
add_executable(test2 ${SOURCE_FILES})
target_link_libraries(test2 library.dll)
It compiled but didnt work. Returns code -1073741515
How can I handle with it?
Although this question is old. You are targeting the link library wrongly.
target_link_libraries(test2 library.dll) is wrong. This is an example linking SDL2. In the main CMakeList.txt
cmake_minimum_required(VERSION 3.12)
project(GraphicTest)
set(CMAKE_CXX_STANDARD 11)
include_directories("${PROJECT_SOURCE_DIR}/SDL")
add_subdirectory(SDL)
add_executable(GraphicTest main.cpp)
target_link_libraries(GraphicTest SDL2)
and in the library folder. Here SDL, add a CMakeLists.txt
message("-- Linking SDL")
add_library(SDL2 SDL2.dll)
set_target_properties(SDL2 PROPERTIES LINKER_LANGUAGE C)

GTest build first at CMake [duplicate]

This question already has answers here:
How to start working with GTest and CMake
(11 answers)
Closed 6 years ago.
I'm building dynamic library and want to use gtest for testing.
SET(GTEST_LIBRARY libs/googletest-master)
set(GTEST_INCLUDE_DIR libs/googletest-master/googletest/include)
#set(GTEST_MAIN_LIBRARY libs/googletest-master/googletest/include/gtest)
#find_package(PostgreSQL REQUIRED)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
But, berofe setting GTEST_MAIN_LIBRARY I have to build it first.
How I can configure CMake to achieve this
Build gtest with Cmake && make (on unix)
Get appropriate path to GTEST_MAIN_LIBRARY
continue build
I copied gtests srcs into project
make a build with Make
and add this to CMake
add_subdirectory(libs/googletest-master)
SET(GTEST_LIBRARY libs/googletest-master)
set(GTEST_INCLUDE_DIR libs/googletest-master/googletest/include)
set(GTEST_MAIN_LIBRARY libs/googletest-master/googlemock/gtest)
enable_testing()
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIR})
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
If your put the source for googletest in a subdirectory parented where your CMakeLists.txt file is, the following should work:
add_subdirectory(./googletest)
add_executable(your_program ${MY_SRC})
add_dependencies(your_program gmock)
add_dependencies(your_program gtest)