Linker error when compiling with Boost.Filesystem on macOS High Sierra - c++

I'm currently trying to compile a program that uses Boost.Filesystem on macOS High Sierra 10.13.4. I'm also using gcc 7.3 to compile, which I manually installed using Homebrew. The program will compile, but then throws the following error during linking
Undefined symbols for architecture x86_64:
"boost::filesystem::path_traits::dispatch(boost::filesystem::directory_entry const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)", referenced from:
boost::enable_if<boost::filesystem::path_traits::is_pathable<boost::decay<boost::filesystem::directory_entry>::type>, boost::filesystem::path&>::type boost::filesystem::path::operator=<boost::filesystem::directory_entry>(boost::filesystem::directory_entry const&) in world.cc.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I'm using cmake to build, and my CMakeLists.txt file looks like this
cmake_minimum_required (VERSION 3.0)
MESSAGE(STATUS "Compiler: " ${CMAKE_CXX_COMPILER})
# variables for CMAKE
set(PROJECT sealab)
set(BINARY ${PROJECT}.out)
set(SRC_DIR src/)
set(INC_DIR inc/)
FILE(GLOB_RECURSE SRC ${SRC_DIR}/*.cc)
# library include dirs
set(IMGUI_INC libs/imgui/)
set(SPDLOG_INC libs/spdlog/include/)
set(JSON_INC libs/json/include/)
set(
LIB_INC
${IMGUI_INC}
${SPDLOG_INC}
${JSON_INC}
${GLM_INC}
)
# library src files
FILE(GLOB IMGUI_SRC ${IMGUI_INC}*.cpp)
FILE(GLOB SPDLOG_SRC ${SPDLOG_INC}*.cpp)
set(
LIB_SRC
${IMGUI_SRC}
${SPDLOG_SRC}
)
project(${PROJECT})
# version number
set(${PROJECT}_VERSION_MAJOR 0)
set(${PROJECT}_VERSION_MINOR 1)
# Compilation Database
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# C++ standard
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-unused-local-typedefs")
# threads
set(THREADS_PREFER_PTHREAD_FLAG ON)
# adding packages
find_package(OpenGL REQUIRED)
find_package(GLEW REQUIRED)
find_package(glfw3 3.2 REQUIRED)
find_package(Threads REQUIRED)
find_package(Assimp REQUIRED)
find_package(glm REQUIRED)
find_package(Boost COMPONENTS system filesystem REQUIRED)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_STATIC_RUNTIME ON)
# adding directories and source files
include_directories(
${OPENGL_INCLUDE_DIRS}
${GLEW_INCLUDE_DIRS}
${ASSIMP_INCLUDE_DIRS}
${GLM_INCLUDE_DIRS}
${Boost_INCLUDE_DIRS}
)
include_directories(${INC_DIR} ${LIB_INC})
link_directories(${INC_DIR} ${LIB_INC})
# adding source files
add_executable(${BINARY} ${SRC} ${LIB_SRC})
set_target_properties(${BINARY} PROPERTIES COTIRE_UNITY_TARGET_NAME "unity")
# linking libraries
target_link_libraries(
${BINARY}
OpenGL::GL
${GLEW_LIBRARIES}
Threads::Threads
${ASSIMP_LIBRARIES}
glfw
glm
${Boost_LIBRARIES}
)
I'm using the command cmake -DCMAKE_C_COMPILER=$CC -DCMAKE_CXX_COMPILER=$CXX . to generate the Makefile, and the environment variables $CC and $CXX are set to the versions of gcc and g++ created by Homebrew.
I've looked at several other stackoverflow answers where people have the same error, and all of those are the result of incorrect linking. I'm linking in the same way that the correct answers say to do, yet the error still persists. Am I still linking it incorrectly or is there something else going on here?

So I found out what was going on. This made me realized that maybe the bottled homebrew Boost 1.66.0 library wasn't built with GCC. I switched the compiler to clang, and now it compiles just fine.

Related

undefined reference to boost::system::generic_category() cmake

I have an undefined reference while trying to compile my Qt5 project.
I am usually pretty confident with using CMake but this time I have a strange error that I can't figure out:
undefined reference to boost::system::generic_category()
/usr/local/include/boost/system/error_code.hpp:223: undefined reference to 'boost::system::generic_category()'
My configurations are:
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Qt5.15
cmake version 3.16.3
After typing whereis boost the outcome wasboost: /usr/include/boost and after applying the great power of dpkg -s libboost-dev | grep 'Version' :) the version is Version: 1.71.0.0ubuntu2
I don't understand what is happening, below an example of how my CMakeLists.txt is structured:
cmake_minimum_required (VERSION 3.1)
project(projectA)
set (OpenCV_DIR /home/to/opencv/build)
find_package( OpenCV REQUIRED )
find_package( Boost COMPONENTS system thread filesystem REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
INCLUDE_DIRECTORIES(${Boost_INCLUDE_DIR})
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
find_package(Qt5Widgets)
find_package(Qt5PrintSupport)
#make them into headers
qt5_wrap_ui (UI_HDRS ${UI})
add_executable(projectA main/main.cpp ui/qdarkstyle/style.qrc ${SRCS} ${UI_HDRS} ${UI_SRCS})
target_link_libraries (projectA Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS} Qt5::PrintSupport)
add_library(projectA_lib SHARED ${SRCS} ${UI_HDRS})
target_include_directories (projectA_lib PUBLIC "src/" "ui/")
link_directories(${Boost_LIBRARY_DIRS})
target_link_libraries (projectA_lib Qt5::Widgets ${Boost_LIBRARIES} ${OpenCV_LIBS})
I have searched and applied solutions I saw on all possible sources I was able to find such as:
This source but that didn't work.
Also from here it seems that this solution shall be applied:
set(Boost_USE_STATIC_LIBS ON)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
find_package(Boost REQUIRED COMPONENTS system)
# the call to include_directories is now useless:
# the Boost::system imported target used below
# embeds the include directories
project(APP C CXX)
add_executable(APP src.cpp)
target_link_libraries(APP Boost::system)
However that also didn't do any specific benefits to finding the solution.
Other posts I consulted were this, this one but no answer was provided.
This post was useful but that also didn't provide any advice that didn't already know.
Thanks for pointing to the right direction and trying to find a solution.

Linking a C library (tree-sitter) into a C++ project cmake issues

I'm trying to get a C library called tree-sitter working with my C++ project.
This is what my project structure looks like:
IDEProject
cmake-build-debug
Headers
Sources
tree-sitter (The library I want to include)
tree-sitter-json (The library that provides a parser I need to use with tree-sitter)
CMakeLists.txt
My CMakeLists.txt looks like this:
cmake_minimum_required(VERSION 3.1.0)
project(IDEProject)
set (CMAKE_PREFIX_PATH "/Users/Chloe/Qt/5.12.0/clang_64")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
# Find includes in corresponding build directories
find_package(Qt5 REQUIRED COMPONENTS Core Widgets Gui Test)
include_directories(Headers)
include_directories(Sources)
file(GLOB project_headers Headers/*.h)
file(GLOB project_sources Sources/*.cpp)
file(GLOB project_forms Forms/*.ui)
# qt_wrap_cpp(project_headers_wrapped ${project_headers})
set(CMAKE_INCLUDE_CURRENT_DIR ON)
# Instruct CMake to run moc automatically when needed
set(CMAKE_AUTOMOC ON)
# Create code from a list of Qt designer ui files
# set(CMAKE_AUTOUIC ON)
add_executable(IDEProject ${project_headers} ${project_sources} ${project_forms})
# Use the Widgets module from Qt 5
target_link_libraries(IDEProject PUBLIC Qt5::Core Qt5::Widgets Qt5::Gui)
add_library(tree-lib tree-sitter/lib/src/lib.c)
include_directories(
tree-sitter/lib/src
tree-sitter/lib/include
tree-sitter/lib/utf8proc
)
target_link_libraries(IDEProject PRIVATE tree-lib)
After compiling that, I get this error:
Undefined symbols for architecture x86_64:
"tree_sitter_json()", referenced from:
PlainTextEdit::onTextChanged() in TextEditor.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The code that my program tries to run is this:
TSLanguage *tree_sitter_json();
TSParser *parser = ts_parser_new();
ts_parser_set_language(parser, tree_sitter_json());
NOTE: All the info regarding tree-sitter is taken from this website: https://tree-sitter.github.io/tree-sitter/using-parsers#an-example-program
Help would really really be appreciated since I've been trying to solve this problem for over 2 days now

Non-default compilers (GCC or Vanilla-Clang) on macOS High Sierra linking issue

I am working on a currently mac based hobby project that involves some parallelization, for which I'd like to use OpenMP since this seems quite portable and relatively easy to implement in my case. After some online reading, I found out that gcc and clang can both compile with -fopenmp in their current versions. The default macOS compiler included with Xcode is stripped of this functionality, though. So, I installed both gcc 7 and clang 5 on my macOS. Since my project is built via CMake, I have written the following makefile.
cmake_minimum_required(VERSION 3.0)
project(Project C CXX)
#SET(CMAKE_C_COMPILER /usr/local/bin/gcc-7)
#SET(CMAKE_CXX_COMPILER /usr/local/bin/gcc-7)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -O2 -mavx2 -march=haswell -flto=thin -g0")
SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread")
set(Boost_USE_STATIC_LIBS OFF)
set(Boost_USE_MULTITHREADED ON)
set(Boost_USE_STATIC_RUNTIME OFF)
FIND_PACKAGE(Boost 1.65 COMPONENTS filesystem system thread atomic chrono regex)
FIND_PACKAGE(OpenCV REQUIRED core imgproc imgcodecs calib3d)
set(HDF5_USE_STATIC_LIBRARIES ON)
set(HDF5_ROOT /usr/local/HDF_GROUP/HDF5/1.10.0/)
FIND_PACKAGE(HDF5 REQUIRED)
include_directories(${CMAKE_SOURCE_DIR}/include ${Boost_INCLUDE_DIRS} ${OpenCV_INCLUDE_DIRS} ${HDF5_INCLUDE_DIRS})
add_executable(Project /path/to/many/files..)
TARGET_LINK_LIBRARIES(Project ${Boost_LIBRARIES})
TARGET_LINK_LIBRARIES(Project ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES(Project ${HDF5_LIBRARIES})
In this version with the CMAKE_CXX_COMPILER setting disabled, the code compiles, links, and runs without a hitch. As soon as I set CMake to use gcc or vanilla-clang for the exact same project without any further changes, compiling finishes without any trouble but linking fails. The linker reports hundreds of missing symbols ranging from a few OpenCV symbols to mostly libc++ things like
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in main.cpp.o
Therefore, I am obviously missing something fundamental. With vanilla-clang, I tried adding -L/usr/local/opt/llvm/lib to point the linker to the libs that came with clang. Unfortunately, this changed nothing.
I'd appreciate any hints on the matter. Thank you
I had the same problem and it took one complete day to find the solution. I am using Mac-Sierra 10.13.4. I want to use Opencv3 (I think the same problem also appears for opencv2) and openMP. I was actually using Clion as the IDE (CLion uses cmake to configure the project unlike other IDE), so I have to write the CMakeLists.txt file.
There was a conflict of using gcc as the compiler for openCV and openMP. If you use gcc as the compiler then it gives error for opencv as :
imwrite() on OS X error: Undefined symbols
You need to specifically use llvm compiler on OS X to resolve this issue. Following, I am giving the correct code for using OpenCV and OpenMP on Mac-Sierra:
cmake_minimum_required(VERSION 3.10)
project(MyOpenCVTest)
set(CMAKE_CXX_STANDARD 11)
add_executable(MyOpenCVTest main.cpp)
# set("OpenCV_DIR" "/modules/opencv/3.4.1/share/OpenCV/")
set(CMAKE_PREFIX_PATH "/usr/local/Cellar/opencv#3/")
set(OpenCV_INCLUDE_DIRS "/usr/local/Cellar/opencv#3/include/")
set(OpenCV_LIBS "/usr/local/Cellar/opencv#3/lib/")
find_package(OpenCV REQUIRED)
message(STATUS "OpenCV library status:")
message(STATUS " version: ${OpenCV_VERSION}")
message(STATUS " libraries: ${OpenCV_LIBS}")
message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
if(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()
set(CMAKE_C_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang")
set(CMAKE_CXX_COMPILER "/usr/local/Cellar/llvm/6.0.0/bin/clang++")
set(OPENMP_LIBRARIES "/usr/local/Cellar/llvm/6.0.0/lib")
set(OPENMP_INCLUDES "/usr/local/Cellar/llvm/6.0.0/include")
OPTION (USE_OpenMP "Use OpenMP to enamble <omp.h>" ON)
# Find OpenMP
if(APPLE AND USE_OpenMP)
if(CMAKE_C_COMPILER_ID MATCHES "Clang")
set(OpenMP_C "${CMAKE_C_COMPILER}")
set(OpenMP_C_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_C_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_C_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_C_LIB_NAMES})
endif()
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(OpenMP_CXX "${CMAKE_CXX_COMPILER}")
set(OpenMP_CXX_FLAGS "-fopenmp=libomp -Wno-unused-command-line-argument")
set(OpenMP_CXX_LIB_NAMES "libomp" "libgomp" "libiomp5")
set(OpenMP_libomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libgomp_LIBRARY ${OpenMP_CXX_LIB_NAMES})
set(OpenMP_libiomp5_LIBRARY ${OpenMP_CXX_LIB_NAMES})
endif()
endif()
if(USE_OpenMP)
find_package(OpenMP REQUIRED)
endif(USE_OpenMP)
if (OPENMP_FOUND)
include_directories("${OPENMP_INCLUDES}")
link_directories("${OPENMP_LIBRARIES}")
set (CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
# set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${OpenMP_EXE_LINKER_FLAGS}")
endif(OPENMP_FOUND)
include_directories( ${OpenCV_INCLUDE_DIRS} )
target_link_libraries( MyOpenCVTest ${OpenCV_LIBS})
TARGET_LINK_LIBRARIES(MyOpenCVTest opencv_core opencv_highgui opencv_imgproc opencv_imgcodecs)
You might want to set e.g. set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -lpthread") such that the linker automatically detects the appropriate pthread library
Do
brew reinstall llvm
to install llvm compiler.
Please note that, you can't use gcc compiler on Mac-Sierra for your project which needs openMP and also openCV. You need to use llvm compiler.
Verify the correct locations of llvm and opencv installation directory.
If using the gcc compiler driver to perform the link change the compiler to 'g++' from 'gcc', g++ automatically brings in the standard c++ library where gcc does not, or add -lstdc++, there may be others required but using g++ as the compiler driver is usually easier.

CPPREST SDK on CLion using CMake on Mac

I am attempting to use the CPPREST SDK in CLion on a Mac using CMake. I'm almost there, but I cannot seem to resolve the following linker error:
Undefined symbols for architecture x86_64:
"_ERR_remove_thread_state", referenced from:
boost::asio::ssl::detail::openssl_init_base::do_init::~do_init() in PongRemote.cpp.o
I have specified -lssl and -lcrypto, but still get this "thread state" error. Based on some research, it looks like it is coming from OpenSSL. I used Homebrew to install CPPREST.
I have tested this with literally just a main and the basic includes:
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
with a CMake of:
project(cpprest)
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 14)
# BOOST PACKAGE
find_package(Boost REQUIRED COMPONENTS
atomic
chrono
date_time
exception
filesystem
random
regex
serialization
system
thread
)
include_directories(${Boost_INCLUDE_DIRS})
IF (!Boost_FOUND)
MESSAGE("*** ERROR *** Boost package not found")
RETURN()
ENDIF ()
# Microsoft RESTful API Package (Casablanca)
set(CPPREST_LIBRARIES "/usr/local/opt/openssl/lib")
include_directories("/usr/local/opt/openssl/include")
# Compile and link
# Build the core library and executable
include_directories(${CMAKE_SOURCE_DIR})
set(SOURCE_FILES
main.cpp
)
set(LINK_LIBRARIES
${Boost_LIBRARIES}
${CPPREST_LIBRARIES}
-lssl
-lcrypto
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})
After taking some time, the linking problem was due to the CMake find commands not working properly. I manually pointed directly to the libraries for both OpenSSL and RESTCPP and the problem was fixed.
Project(cpprest)
cmake_minimum_required(VERSION 3.8)
set(CMAKE_CXX_STANDARD 17)
# BOOST PACKAGE
set(Boost_USE_MULTITHREADED ON) # Default ON
set(Boost_USE_STATIC_LIBS ON) # Default OFF
set(Boost_USE_DEBUG_RUNTIME OFF) # Default ON
set(Boost_USE_DEBUG_PYTHON OFF) # Default OFF
set(Boost_USE_STLPORT OFF) # Default OFF
find_package(Boost REQUIRED COMPONENTS
atomic
chrono
date_time
exception
filesystem
program_options
random
regex
system
serialization
thread
)
IF (!Boost_FOUND)
MESSAGE("*** ERROR *** Boost package not found")
RETURN()
ENDIF ()
include_directories(${Boost_INCLUDE_DIRS})
MESSAGE("Boost_INCLUDE_DIRS:\t" ${Boost_INCLUDE_DIRS})
# Open SSL Package
set(OpenSSL_INCLUDE /usr/local/opt/openssl/include)
set(OpenSSL_LIBRARIES
/usr/local/opt/openssl/lib/libcrypto.dylib
/usr/local/opt/openssl/lib/libssl.dylib)
include_directories(${OpenSSL_INCLUDE})
# Microsoft RESTful API Package (Casablanca)
set(CppREST_INCLUDE /usr/local/opt/cpprestsdk/include)
set(CppREST_LIBRARIES /usr/local/opt/cpprestsdk/lib/libcpprest.dylib)
include_directories(${CppREST_INCLUDE})
# Compile and link
# Build the core library and executable
set(SOURCE_FILES main.cpp)
set(LINK_LIBRARIES
${Boost_LIBRARIES}
${OpenSSL_LIBRARIES}
${CppREST_LIBRARIES}
)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})
target_link_libraries(${PROJECT_NAME} ${LINK_LIBRARIES})

CMake not linking SDL library? [duplicate]

I'm looking for the simplest way to compile a c++ program using SDL2 and SDL_image with cmake.
Here is my best attempt, after hours of searching:
CMakeLists.txt
project(shooter-cmake2)
cmake_minimum_required(VERSION 2.8)
set(SOURCES
shooter.cpp
classes.cpp
utils.cpp
)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(${PROJECT_NAME} ${SOURCES})
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2_image REQUIRED sdl2_image)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIR})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARY})
I get these errors:
In function `loadTexture(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, SDL_Renderer*)':
undefined reference to `IMG_LoadTexture'
collect2: ld returned 1 exit status
Here is the function call:
#include "SDL.h"
#include "SDL_image.h"
SDL_Texture* loadTexture(const std::string &file, SDL_Renderer *ren){
SDL_Texture *texture = IMG_LoadTexture(ren, file.c_str());
texture != nullptr or die("LoadTexture");
return texture;
}
I think that the following will work, as it finds the libraries on my ubuntu system and the example function you provided can link:
project(shooter-cmake2)
cmake_minimum_required(VERSION 2.8)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++0x")
add_executable(${PROJECT_NAME} src/test.cpp)
INCLUDE(FindPkgConfig)
PKG_SEARCH_MODULE(SDL2 REQUIRED sdl2)
PKG_SEARCH_MODULE(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
INCLUDE_DIRECTORIES(${SDL2_INCLUDE_DIRS} ${SDL2IMAGE_INCLUDE_DIRS})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${SDL2_LIBRARIES} ${SDL2IMAGE_LIBRARIES})
If cmake is executed with --debug-output it outputs:
-- Found PkgConfig: /usr/bin/pkg-config (found version "0.26")
Called from: [2] /usr/share/cmake-2.8/Modules/FindPkgConfig.cmake
[1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
-- checking for one of the modules 'sdl2'
Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
-- checking for one of the modules 'SDL2_image>=2.0.0'
Called from: [1] $USER/stack-overflow/cmake-sdl2-image/CMakeLists.txt
This made me check the contents of
/usr/lib/x86_64-linux-gnu/pkgconfig/sdl2.pc
/usr/lib/x86_64-linux-gnu/pkgconfig/SDL2_image.pc
I noticed that SDL2_image.pc contains
Name: SDL2_image
which I assumed should match the third parameter to PKG_SEARCH_MODULE for this library.
There are two blog posts about this here:
Using SDL2 with CMake
Using SDL2_image with CMake
Basically you need a FindSDL2.cmake and FindSDL2_image.cmake module. They can be based of the ones that work for SDL 1.2 which are included in CMake already. Using these Find modules will also work on Windows.
If you are on Linux and only need SDL2 you don't even need the FindSDL2.cmake as the following already works:
cmake_minimum_required(VERSION 3.7)
project(SDL2Test)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
add_executable(SDL2Test Main.cpp)
target_link_libraries(SDL2Test ${SDL2_LIBRARIES})
I was having trouble with these answers, I think cmake changed the way to import targets. Following #trenki blog post I needed to change my CMakeLists.txt to:
project(SDL2Test)
find_package(SDL2 REQUIRED COMPONENTS SDL2::SDL2)
add_executable(SDL2Test main.cpp)
target_link_libraries(SDL2Test SDL2::SDL2)
Currently this works out of the box on Arch Linux.
I introduced a modern and portable approach for linking to the SDL2, SDL2_image. These SDL2 CMake modules let you build an SDL2 & SDL2_image project as follows :
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake/sdl2)
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
target_link_libraries(${PROJECT_NAME} SDL2::Main SDL2::Image)
You should just clone the repo in your project:
git clone https://github.com/aminosbh/sdl2-cmake-modules cmake/sdl2
Note: If CMake didn't find the SDL2/SDL2_image libraries (in Windows), we can specify the CMake options SDL2_PATH and SDL2_IMAGE_PATH as follows:
cmake .. -DSDL2_PATH="/path/to/sdl2" -DSDL2_IMAGE_PATH="/path/to/sdl2-image"
It supports also other related libraries : SDL2_ttf, SDL2_net, SDL2_mixer and SDL2_gfx. For more details, please read the README.md file.
You can find a list of examples/samples and projects that uses these modules here : https://github.com/aminosbh/sdl-samples-and-projects
Since 2.6 version, SDL2_image installation is shipped with CMake config script SDL2_imageConfig.cmake/SDL2_image-config.cmake.
So find_package(SDL2_image) works without any additional FindSDL2_image.cmake module, and creates IMPORTED target SDL2_image::SDL2_image:
find_package(SDL2_image REQUIRED)
target_link_libraries(<executable-target> SDL2_image::SDL2_image)
Note, that variables like SDL2_IMAGE_LIBRARIES or SDL2_IMAGE_INCLUDE_DIRS are NOT set in this case, so using them is meaningless.
The following commands works fine for me:
set(SDL_INCLUDE_DIR "/usr/include/SDL2")
set(SDL_LIBRARY "SDL2")
include(FindSDL)
if(SDL_FOUND)
message(STATUS "SDL FOUND")
endif()