I have two C++ projects in SDL, both using SDL. For whatever reason, one of these projects can find SDL just fine. For the other, I can write code as if SDL was found- with autocompletion and everything- but when it comes to building the project, I am informed that fatal error: 'SDL2/SDL.h' file not found.
The CMakeLists.txt files of both projects are basically identical.
CMakeLists.txt for the project that doesn't work:
cmake_minimum_required(VERSION 3.12)
project(Snake)
set(CMAKE_CXX_STANDARD 11)
include_directories(.)
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME}
main.cpp
SnakeBlock.cpp
SnakeBlock.h
SnakeGame.cpp
SnakeGame.h
SnakeBoard.cpp
SnakeBoard.h)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
And CMakeLists.txt for the project that does work:
cmake_minimum_required(VERSION 3.12)
project(Tetris)
set(CMAKE_CXX_STANDARD 11)
include_directories(.)
find_package(SDL2 REQUIRED)
add_executable(${PROJECT_NAME}
Game.cpp
Game.hpp
main.cpp
Playfield.cpp
Playfield.hpp
Tetromino.cpp
Tetromino.hpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY})
And for both projects, SDL2 appears under the "External Libraries" section on the right sidebar, under Header Search Paths.
What gives?
Related
I am trying to create a library on the top of SDL2. For the compilation I'm using cmake.
First I had this CMakeLists.txt for the library :
cmake_minimum_required(VERSION 3.10)
project(SquareEngine)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake/")
find_package(SDL2 REQUIRED)
find_package(SDL2_image REQUIRED)
include_directories(${SDL2_INCLUDE_DIRS})
include_directories(${SDL2_IMAGE_INCLUDE_DIRS})
set (SRCS Main.cpp
Rectangle.cpp
Scene.cpp
SquareEngine.cpp)
set (HEADERS Rectangle.hpp
Scene.hpp
SquareEngine.hpp
utils/Color.hpp
utils/Vector.hpp)
add_library(SquareEngine ${SRCS} ${HEADERS} ${SDL} ${SDL_SRC})
target_link_libraries(SquareEngine ${SDL2_LIBRARIES} ${SDL2_IMAGE_LIBRARIES})
To include the SDL2 and SDL2 image I follow this instructions.
When I try to build it's work perfectly.
Now I try to use my library in a project. I create my own project and I link my librairy that is located in a subfolder /SquareEngine in my project. For the project I use this CMakeLists.txt :
cmake_minimum_required(VERSION 3.10)
project(Demo)
set (SRCS Main.cpp)
add_subdirectory(SquareEngine)
add_executable(Demo ${SRCS})
target_link_libraries(Demo PUBLIC SquareEngine)
target_include_directories(Demo PUBLIC SquareEngine)
When I try to build the project I got an error like this :
Cannot open include file: 'SDL.h': No such file or directory
I try to figure out for long time and I don't have any clue why.
Maybe it will be better to use the SDL in static rather than shared ?
I think you need to add the SDL include dirs to the SquareEngine library:
target_include_directories(SquareEngine PUBLIC ${SDL2_INCLUDE_DIRS} ${SDL2_IMAGE_INCLUDE_DIRS})
The most recent SDL2 releases provide SDL2::* CMake targets such that you don't need to fiddle with CMake variables anymore.
So there's no more need to ship your custom FindSDL2.cmake and/or FindSDL2_image.cmake modules.
With those, you can rewrite your cmake script as:
cmake_minimum_required(VERSION 3.10)
project(SquareEngine)
find_package(SDL2 REQUIRED CONFIG)
find_package(SDL2_image REQUIRED CONFIG)
set (SRCS
Main.cpp
Rectangle.cpp
Scene.cpp
SquareEngine.cpp
)
set (HEADERS
Rectangle.hpp
Scene.hpp
SquareEngine.hpp
utils/Color.hpp
utils/Vector.hpp
)
add_library(SquareEngine ${SRCS} ${HEADERS})
target_link_libraries(SquareEngine PRIVATE SDL2::SDL2 SDL2_image::SDL2_image)
target_include_directories(SquareEngine
PUBLIC
$<TARGET_PROPERTY:SDL2::SDL2,INTERFACE_INCLUDE_DIRECTORIES>
$<TARGET_PROPERTY:SDL2_image::SDL2_image,INTERFACE_INCLUDE_DIRECTORIES>
"$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>"
)
It can then be used in your game as:
cmake_minimum_required(VERSION 3.10)
project(Demo)
set (SRCS Main.cpp)
add_subdirectory(SquareEngine)
add_executable(Demo ${SRCS})
target_link_libraries(Demo PUBLIC SquareEngine)
|---Engine
|----CMakeList.txt
|----Engine.cpp
|----Engine.h
|---Models
|----CMakeList.txt
|----Snake.cpp
|----Snake.hpp
|---Type
|----CMakeList.txt
|----Point.hpp
|---CMakeList.txt
|---main.cpp
I just can't tell the main sheet that I want to add a directory with files to it ...
Each time, errors of the following type appear:
CMake Error at CMakeLists.txt: 12 (target_include_directories):
Cannot specify include directories for target "Snake" which is not built by
this project.
CMake Error at CMakeLists.txt: 13 (target_link_directories):
Cannot specify link directories for target "Snake" which is not built by
this project.
CMake Error at Engine / CMakeLists.txt: 8 (target_include_directories):
Cannot specify include directories for target "Snake" which is not built by
this project.
CMake Error at CMakeLists.txt: 17 (target_link_libraries):
Cannot specify link libraries for target "Snake" which is not built by this
project.
- Configuring incomplete, errors occurred!
Which, for several hours of intensified attempts, are not corrected in any way. Can anyone help with this?
CMakeLists.txt from Engine folder:
set(HEADERS
Engine.h)
set(SOURCES
Engine.cpp)
add_library(Engine ${HEADERS} ${SOURCES})
target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_LIST_DIR})
Main CMakeList.txt:
cmake_minimum_required(VERSION 3.16.3)
project(Snake)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(PROJECT_SOURCES
main.cpp
)
target_include_directories(${PROJECT_NAME} PUBLIC Engine)
target_link_directories(${PROJECT_NAME} PUBLIC Engine)
add_subdirectory(Engine)
target_link_libraries(${PROJECT_NAME} Engine)
add_executable(${PROJECT_NAME}
${PROJECT_SOURCES}
)
find_package(SFML 2.5.1 COMPONENTS graphics audio REQUIRED)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-audio)
set(SFML_STATIC_LIBRARIES FALSE)
There are two errors. The first one is to call a PROJECT_NAME that hasn't been built yet. add_executable(${PROJECT_NAME} ...) and add_library(${PROJECT_NAME ...) should be used before the target_include_directories and target_link_directories that refers to it. Because, as #Stephen Newell noted, otherwise you will be using a PROJECT NAME that hasn't been declared/built yet.
The second error is that you are calling a PROJECT_NAME inside Engine/CMakeLists. Maybe you are missing a project(...) inside of it.
Maybe you should move set(SFML_STATIC_LIBRARIES FALSE) before linking it, as well.
I'm trying to learn OpenGL from a tutorial, and I need to link SOIL2 to my project.
I tried to write my CMake like this:(all other libs are working, Cmake gets no error but I can't include SOIL2.h because of 'SOIL2.h' file not found). I'm working with Clion IDE.
cmake_minimum_required(VERSION 3.15)
project(OpenGL_Tutorial)
set(CMAKE_CXX_STANDARD 14)
add_executable(OpenGL_Tutorial main.cpp)
find_package(OpenGL REQUIRED)
find_package(SDL2 REQUIRED)
find_package(PkgConfig REQUIRED)
pkg_search_module(GLFW REQUIRED glfw3)
find_library(SOIL2 lib/libsoil2.a)
include_directories(
${OPENGL_INCLUDE_DIRS}
${GLEW_INCLUDE_DIRS}
${GLFW_INCLUDE_DIRS}
${SDL2_INCLUDE_DIR}
)
target_link_libraries(OpenGL_Tutorial
${OPENGL_LIBRARY}
${GLEW_LIBRARY}
${GLFW_LIBRARIES}
${SDL2_LIBRARY}
${SOIL2_LIBRARY}
)
libsoil2.a is located in my lib directory in the project directory
(see project direcory image)
Thanks!
I make a libtest_lib.a file with cmake.
cmake_minimum_required(VERSION 3.8)
project(test)
set(CMAKE_CXX_STANDARD 98)
set(SOURCE_FILES library.cpp library.h)
add_library(test_lib ${SOURCE_FILES})
then in my executable C++ project ,I include the #include "library.h"
and the CMakeList.txt:
cmake_minimum_required(VERSION 3.8)
project(study)
set(CMAKE_CXX_STANDARD 98)
set(SOURCE_FILES main.cpp)
add_executable(study ${SOURCE_FILES})
target_link_libraries(study libtest_lib.a) //libtest_lib.a file under the project path
but it fails.
/Users/bin381/CLionProjects/study/main.cpp:1:10: fatal error: 'library.h' file not found
Please add the include directories as mentioned in the documentation : https://cmake.org/cmake/help/v3.0/command/include_directories.html
Otherwise cmake will not be able to find where to look in for include files.
I am currently trying to build wxWidgets-3.1.0 on a CLion 1.3 project. I use Ubuntu 16.04 (64 bit). Basically, I edited the CMakeLists.txt file like this:
cmake_minimum_required(VERSION 3.5)
project(WxProva)
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules"
${CMAKE_MODULE_PATH})
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(WxProva ${SOURCE_FILES})
find_package(wxWidgets)
include_directories(${wxWidgets_INCLUDE_DIRS})
target_link_libraries(WxProva ${wxWidgets_LIBRARIES})
The "External Libraries" section also shows me wxWidgets, but when it comes to write some lines on my main.cpp, everything related with the library seems to be unreachable by the compiler (it's all written in red, like an error). Anyway, if I try to compile, that's the result:
/home/federico/ClionProjects/WxProva/main.cpp:2:35: fatal error: wxWidgets-3.1.0/include: File o directory non esistente
compilation terminated.
Which is like "File or directory doesn't exists."
How can I fix this?
After some experiments here solution. You can just copy it and change some information and ready to build and run.
cmake_minimum_required(VERSION 3.7)
project(Your_Project_Name) //any name for your project
set(CMAKE_CXX_STANDARD 11)
set(wxWidgets_ROOT_DIR </usr/include/wx-3.0-unofficial>) // here I am giving where to search for wxwidgets library. it can be different for you
set(wxWidgets_CONFIGURATION mswu)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
set(SOURCE_FILES main.cpp)
add_executable(FirstC ${SOURCE_FILES})
target_link_libraries(FirstC ${wxWidgets_LIBRARIES})
For more Information read https://wiki.wxwidgets.org/CMake
Edit 1
Here you shouldn't even add some compile and link config (wx-config --cxxflags and wx-config --libs) as it is necessary in NetBeans
Here is example configuration for macOS 10.14.4 (Mojave) and CLion 2019.1
(/usr/local is folder where I installed wxWidgets)
cmake_minimum_required(VERSION 3.14)
project(wx1Test)
set(CMAKE_CXX_STANDARD 14)
set(wxWidgets_ROOT_DIR </usr/local/include/wx-3.1>)
set(wxWidgets_CONFIGURATION mswu)
find_package(wxWidgets COMPONENTS core base REQUIRED)
include(${wxWidgets_USE_FILE})
set(SOURCE_FILES main.cpp)
add_executable(wx1Test ${SOURCE_FILES})
target_link_libraries(wx1Test ${wxWidgets_LIBRARIES})