I can't get my cmake project running. It should build a library (Core) and then add this library to a new shared library. The problem is that the resulting library does not seem to link correctly against the executable.
fatal error: JNF_NEAT/body.h: No such file or directory
I have a CMake project which is structured as follows:
root
-> CMakeLists.txt
-> Core/
-> CMakeLists.txt
-> Sources/
-> Examples/
-> CMakeLists.txt
-> Example1/
-> CMakeLists.txt
-> Sources/
root/CMakeLists.txt
cmake_minimum_required(VERSION 3.2.2)
project(JNF_NEAT)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_BINARY_DIR}/out/examples)
add_subdirectory(Core)
add_library(JNF_NEAT SHARED $<TARGET_OBJECTS:Core>)
target_link_libraries(JNF_NEAT -lstdc++fs)
add_subdirectory(Examples)
root/Core/CMakeLists.txt
file(GLOB SOURCES Sources/*.cpp Sources/*.h)
add_library(Core OBJECT ${SOURCES})
root/Examples/CMakeLists.txt
add_subdirectory(XOR)
root/Examples/XOR/CMakeLists.txt
include_directories(../../out/lib)
file(GLOB SOURCES Sources/*.cpp Sources/*.h)
add_executable(XOR_EXAMPLE ${SOURCES})
target_link_libraries(XOR_EXAMPLE JNF_NEAT)
The entire source code is available here.
Edit #1
I tried setting target_include_directories(XOR_EXAMPLE BEFORE PUBLIC ../../out/lib) before target_link_libraries
I also tried setting include_directories(out/lib) in the outer most CMakeLists.txt
Edit #2
On Linux this error occures.
The error
JNF_NEAT/body.h: No such file or directory
is not a link error, but rather a compilation error.
Check that JNF_NEAT/body.h actually exists. If it does, it might not be included in the list of directories where the compiler looks for #includes. You can set this in CMake with the include_directories command.
Add the given directories to those the compiler uses to search for include files. Relative paths are interpreted as relative to the current source directory.
The include directories are added to the INCLUDE_DIRECTORIES directory property for the current CMakeLists file. They are also added to the INCLUDE_DIRECTORIES target property for each target in the current CMakeLists file. The target property values are the ones used by the generators.
By default the directories specified are appended onto the current list of directories. This default behavior can be changed by setting CMAKE_INCLUDE_DIRECTORIES_BEFORE to ON. By using AFTER or BEFORE explicitly, you can select between appending and prepending, independent of the default.
Related
I'm attempting to build a CMakeLists.txt for my project. It needs to include SDL, OpenGL, and GLEW as well as all of my *.cpp and *.h files in different directories. All files used to build the project are in the Project Source Directory that cmake is called in.
The error:
In file included from /home/me/repos/AlchemyEngine/alchemy/alcEngine.cpp:1:0:
/home/me/repos/AlchemyEngine/alchemy/alcEngine.h:69:18: fatal error: glew.h: No such file or directory
#include <glew.h>
I think it'd be easiest to show the steps for setting this project up in Visual Studio (which works as I used to develop from there) and hope I can get help recreating the setup in my CMakeLists.txt file.
Setting up the project in Visual Studio
1. Includes
Project -> *ProjectName* Properties -> VC++ Directories
Click Include Directories -> Click down-arrow- > edit
Browse for the include files (...\SDL2-OpenGL-GLEW-VC++\include)
2. Libraries
Project -> *ProjectName* Properties -> VC++ Directories
Click Library Directories -> Click down-arrow -> edit
Browse for the library files (...\SDL2-OpenGL-GLEW-VC++\lib)
3. Linkers
Project -> *ProjectName* Properties -> Linker -> Input
Click Additional Dependencies -> Click down arrow -> edit
Copy into box:
SDL2.lib
SDL2main.lib
SDL2_image.lib
OpenGL32.lib
glu32.lib
glew32.lib
4. DLLs
Build project with chosen entry point to create Debug folder with executable.
Copy the .dll files from the ...\SDL-OpenGL-GLEW-VC++\dropInExe folder and paste into the
Debug folder with the project executable
(...\*Project Folder*\Debug)
Here is my CMakeLists.txt file as it stands
cmake_minimum_required (VERSION 3.5)
project (AlchemyEngine)
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
include_directories (
alchemy/
alchemy/event/
alchemy/scene/
alchemy/resource/
alchemy/render/
alchemy/util/
game/
#SDL2_OpenGL_GLEW_VC++/include
)
set( srcs
alchemy/alcEngine.cpp
alchemy/event/alcEvent.cpp
alchemy/render/alcRenderer.cpp
alchemy/render/alcSprite.cpp
alchemy/resource/alcResource.cpp
alchemy/resource/alcShader.cpp
alchemy/resource/alcTexture.cpp
alchemy/scene/alcObject.cpp
alchemy/scene/alcScene.cpp
alchemy/util/alcBlockAllocator.cpp
alchemy/util/alcStackAllocator.cpp
game/Player.cpp
Main.cpp
)
add_library (libraries STATIC SDL2_OpenGL_GLEW_VC++/include/)
add_executable (alc ${PROJECT_SOURCE_DIR}/${srcs})
target_link_libraries(alc libraries)
set_target_properties(libraries PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(alc PROPERTIES LINKER_LANGUAGE CXX)
After configuration and generation, $CMAKE_SOURCE_DIR/build/bin contains:
liblibraries.a
glew32.dll
libpng16-16.dll
SDL2.dll
SDL2_image.dll
zlib1.dll
It seems to me that my method for including the header files is incorrect, but poking around the CMake documentation and other stack exchange posts hasn't produced any better results. I have a feeling it has to do with the linker step, but I cannot figure out how to specify the dependencies in CMake.
I can tree and post the output too if the project structure is that important for this issue.
FINAL EDIT:
The error was fixed in the answer below, here is my final CMakeLists.txt for anyone interested
# Outputs executable(s) in {PROJECT_SOURCE_DIR}/build/bin/
# specify min version and project name
cmake_minimum_required (VERSION 3.5)
project (AlchemyEngine)
# set output paths for binaries/executable
set(CMAKE_BINARY_DIR ${CMAKE_SOURCE_DIR}/build/bin)
set(EXECUTABLE_OUTPUT_PATH ${CMAKE_BINARY_DIR})
set(LIBRARY_OUTPUT_PATH ${CMAKE_BINARY_DIR})
# include project header files
include_directories (
alchemy/
alchemy/event/
alchemy/scene/
alchemy/resource/
alchemy/render/
alchemy/util/
game/
)
# create source files definition
set( srcs
alchemy/alcEngine.cpp
alchemy/event/alcEvent.cpp
alchemy/render/alcRenderer.cpp
alchemy/render/alcSprite.cpp
alchemy/resource/alcResource.cpp
alchemy/resource/alcShader.cpp
alchemy/resource/alcTexture.cpp
alchemy/scene/alcObject.cpp
alchemy/scene/alcScene.cpp
alchemy/util/alcBlockAllocator.cpp
alchemy/util/alcStackAllocator.cpp
game/Player.cpp
Main.cpp
)
# add a library target to be built from the source files
add_library (libraries STATIC SDL2_OpenGL_GLEW_VC++/lib/)
# specify include directories
target_include_directories(libraries PUBLIC SDL2_OpenGL_GLEW_VC++/include/)
# specify libraries to use when linking the library
target_link_libraries (libraries
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/glew32.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/glew32s.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2_image.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2.lib
${PROJECT_SOURCE_DIR}/SDL2_OpenGL_GLEW_VC++/lib/SDL2main.lib
)
# create the executable using the source files defined above
add_executable (alc ${PROJECT_SOURCE_DIR}/${srcs})
# specify the headers
target_include_directories(alc PUBLIC ${PROJECT_SOURCE_DIR})
# link the library to the project
target_link_libraries(alc libraries)
# force the linker language because there's some c stuff in the library files
set_target_properties(libraries PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(alc PROPERTIES LINKER_LANGUAGE CXX)
target_include_directories(libraries PRIVATE SDL2_OpenGL_GLEW_VC++/include/) should be using PUBLIC so that this path is known to any executable target that links against libraries such as target_link_libraries(alc libraries).
This is assuming that glew.h resides in the SDL2_OpenGL_GLEW_VC++/include/ directory.
Since target_include_directories is using a relative path (I'm pretty sure it is relative to the source tree) you need to make sure that this is correct for your directory tree.
I am somewhat new to cmake and completely new to glew, glfw, and the like. I'm following a youtube channel to learn more about game engines and programming.
My problem is linking the static glew library in my project using cmake.
First, I start with the glew source code from http://mcs.une.edu.au/doc/glew-devel/index.html.
I compile it by:
cd build; cmake ./cmake; make glew_s
This adds a lib directory into the build directory with libGLEW.a
A shortened version of my CMakeLists.txt looks like:
cmake_minimum_required (VERSION 3.5 FATAL_ERROR)
project (TestProject CXX)
set(CMAKE_CXX_FLAGS "-std=c++11")
###########################
# GLEW
###########################
add_subdirectory(${CMAKE_SOURCE_DIR}/Dependencies/GLEW)
target_link_libraries(${PROJECT_NAME} glew)
and in Dependencies/GLEW I have another CMakeLists.txt:
# Add glew source and header files
file(GLOB_RECURSE glew-lib ${CMAKE_CURRENT_SOURCE_DIR}/lib/*)
file(GLOB_RECURSE glew-headers ${CMAKE_CURRENT_SOURCE_DIR}/include/GL/*)
add_library(glew ${glew-lib} ${glew-headers})
target_include_directories(glew PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include/GL")
I put a copy of the libGLEW.a file into the lib directory and the include directory is copied from the glew source code include directory. It holds the GL directory, which contains the header files glew.h, wglew.h, eglew.h, and glxew.h.
When I run cmake I get the error:
CMake Error: Cannot determine link language for target "glew".
CMake Error: CMake can not determine linker language for target: glew
The glew source code also has a src directory with glew.c in it, but if I put it in the libs directory and include it in the Dependencies/GLEW/CMakeLists.txt like:
# Add glew source and header files
file(GLOB_RECURSE glew-lib ${CMAKE_CURRENT_SOURCE_DIR}/lib/*.c)
file(GLOB_RECURSE glew-headers ${CMAKE_CURRENT_SOURCE_DIR}/include/GL/*)
add_library(glew ${glew-lib} ${glew-headers})
target_include_directories(glew PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
I get the error:
CMake Error: Error required internal CMake variable not set, cmake may
be not be built correctly.
Missing variable is: CMAKE_C_COMPILE_OBJECT
CMake Error: Error required internal CMake variable not set, cmake may
be not be built correctly.
Missing variable is: CMAKE_C_CREATE_STATIC_LIBRARY
Lastly, I have tried just including the glew.c and headers in the root CMakeLists.txt like:
#########################
# GLEW
#########################
include_directories("${CMAKE_SOURCE_DIR}/Dependencies/GLEW/lib/")
include_directories("${CMAKE_SOURCE_DIR}/Dependencies/GLEW/include/GL/")
Here cmake will finish, but it won't be able to compile, saying classes do not name a type and/or are not declared in this scope.
Any help would be appreciated. I was under the impression that the libGLEW.a was the static library, and all I would have to do is link and compile it along with the headers, but that did not work.
First of all, I forgot to use
make install
after using make the first time, so I was using the incorrect libGLEW.a file.
After including the proper libGLEW.a file, my directory structure had
./Dependencies/GLEW/include/GL/*.h //header files
./Dependencies/GLEW/lib/libGLEW.a //source file
Finally, the TopLevel CMakeLists.txt is changed to include:
add_library(glew STATIC IMPORTED GLOBAL)
set_target_properties(glew PROPERTIES IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Dependencies/GLEW/lib/libGLEW.a )
set_target_properties(glew PROPERTIES INCLUDE_DIRECTORIES ${CMAKE_SOURCE_DIR}/Dependencies/GLEW/include )
If I want to include the library from a lower CMakeLists.txt such as /Dependencies/GLEW/CMakeLists.txt Then I would have to first export the library from there and then import it at the topLevel CMakeLists.txt file.
Now, in order to use glew headers in my project I can just use #include .
If I have .h and .cpp files in the directory src, where the .cpp files include the .h files, using these commands in CMake:
aux_source_directory(src SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
And opening that CMake file in Qt Creator, gets all the files (sources + headers) in the list of project files (the file tree on the left by default).
Now, on the other hand, if I put all the .h files in a directory include, and use this:
include_directories(include)
aux_source_directory(src SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
The header files disappear from the project files!
How can I keep the header files in that directory, and still have them listed in Qt Creator's project files?
You shouldn't use aux_source_directory() for your task. That command is for something different. Just list the source files (or put them in a variable).
You shouldn't use include_directory() for defining include directories any more. This command will just populate the -I flag of the compiler. Define a variable with the header files and add that to the executable.
In case you don't want to list every file manually, use file(GLOB ...). But be aware of the caveats mentioned frequently all over the web with using that command.
Afterwards, tell CMake to populate the -I flag only for that executable with the include directory. That way, other targets don't get polluted by includes, they shouldn't use.
set(SOURCES
src/main.cpp
src/whatever.cpp)
set(HEADERS
include/whatever.h)
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
target_include_directories(${PROJECT_NAME} PUBLIC include)
I add my header files always explicit to avoid any surprise.
But on MacOS using QtCreator 4.2.0 and cmake 3.7.1 I can't reproduce your issue.
However I recommend to use following structure to know which files are within project and to trigger update of cmake's data during update of CMakeLists.txt.
In project/CMakeLists.txt:
add_subdirectory(src)
include_directory(include)
add_executable(foo ${SRC_LIST})
In project/src/CMakeLists.txt:
set(SRC_LIST
${SRC_LIST}
${CMAKE_CURRENT_SOURCE_DIR}/a.cpp
${CMAKE_CURRENT_SOURCE_DIR}/b.cpp
PARENT_SCOPE
)
Here is a sample project I am trying to build with a "Packages" directory which includes all the libraries to be used in the main code.
I am trying to keep my root cmake file as clean as possible and avoid relative path such as
include_directory(packages/lib1)
but I am struggling. Is there a way of including sub-directories of a directory for the purposes of header inclusion.
First a few minor remarks:
always name the CMake configuration files CMakeLists.txt (because of)
bookmark the documentation on CMake: https://cmake.org/documentation/
Sometimes it's not that easy to read, but very specific once you adopt your head to the "CMake world" ;-)
make yourself comfortable with the scope of CMake variables
include_directories(DIR1 [DIR2 [...]])
Tells CMake where the compiler should look for header files, i.e. -IDIR1 -IDIR2 ....
add_library(NAME [STATIC|SHARED] SOURCES)
This command creates the required compiler commands to create a static or shared library out of a given list of source files. No need to add in the header files. The make target will be called NAME and the library target is known to CMake as NAME.
add_subdirectory(DIR)
Tells CMake to look into DIR and parse the included CMakeLists.txt with all its content.
target_link_libraries(TARGET LIB1 [LIB2 [...]])
Tells CMake to instruct the linker to link LIB1, LIB2, etc. to the TARGET, i.e. -LLIB1 -LLIB2 .... TARGET is a CMake/make target previously defined/created with a call to add_{library,executable,custom_target}.
CMakeLists.txt:
include_directories(libraries)
# a header file in `libraries/lib1/foo.hpp` can be included
# in the whole CMake project by `#include "lib1/foo.hpp"`.
add_subdirectory(libraries)
add_subdirectory(tests)
libraries/CMakeLists.txt:
add_subdirectory(lib1)
add_subdirectory(lib2)
libraries/lib1/CMakeLists.txt:
add_library(lib1 STATIC ${LIB1_SOURCES})
libraries/lib2/CMakeLists.txt:
add_library(lib2 STATIC ${LIB2_SOURCES})
tests/CMakeLists.txt:
add_executable(tests ${TEST_SOURCES})
target_link_libraries(tests lib1 lib2)
My code is organized like this:
cpp
main.cpp (calls code from dataStructures/ and common/)
CMakeLists.txt (topmost CMakeLists file)
build
common
CMakeLists.txt (should be responsible for building common shared library)
include
utils.h
src
utils.cpp
build
dataStructures
CMakeLists.txt (build dataStructures shared library - dependent on common library)
include
dsLinkedList.h
src
dsLinkedList.cpp
build
build\ directories contain the built target. The actual code can be seen here: https://github.com/brainydexter/PublicCode/tree/master/cpp
As of now, CMakeLists.txt in each of the subdirectories build their own shared libraries. Topmost CMakeLists file then references the libraries and paths like this
Topmost CMakeLists.txt
cmake_minimum_required(VERSION 3.2.2)
project(cpp)
#For the shared library:
set ( PROJECT_LINK_LIBS libcppDS.dylib libcppCommon.dylib)
link_directories( dataStructures/build )
link_directories( common/build )
#Bring the headers, into the project
include_directories(common/include)
include_directories(dataStructures/include)
#Can manually add the sources using the set command as follows:
set(MAINEXEC main.cpp)
add_executable(testDS ${MAINEXEC})
target_link_libraries(testDS ${PROJECT_LINK_LIBS} )
How can I change the topmost CMakeLists.txt to go into subdirectories (common and dataStructures) and build their targets if they haven't been built, without me having to manually build the individual libraries ?
CMakeLists for common :
cmake_minimum_required(VERSION 3.2.2)
project(cpp_common)
set(CMAKE_BUILD_TYPE Release)
#Bring the headers, such as Student.h into the project
include_directories(include)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
#Generate the shared library from the sources
add_library(cppCommon SHARED ${SOURCES})
dataStructures :
cmake_minimum_required(VERSION 3.2.2)
project(cpp_dataStructures)
set(CMAKE_BUILD_TYPE Release)
#For the shared library:
set ( PROJECT_LINK_LIBS libcppCommon.dylib )
link_directories( ../common/build )
#Bring the headers, such as Student.h into the project
include_directories(include)
include_directories(../common/include/)
#However, the file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
#Generate the shared library from the sources
add_library(cppDS SHARED ${SOURCES})
Update:
This pull request helped me understand the correct way of doing this:
https://github.com/brainydexter/PublicCode/pull/1
and commitId: 4b4f1d3d24b5d82f78da3cbffe423754d8c39ec0 on my git
You are only missing a simple thing: add_subdirectory.
From the documentation:
add_subdirectory(source_dir [binary_dir] [EXCLUDE_FROM_ALL])
Add a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.txt and code files are located. If it is a relative path it will be evaluated with respect to the current directory (the typical usage), but it may also be an absolute path. The binary_dir specifies the directory in which to place the output files. If it is a relative path it will be evaluated with respect to the current output directory, but it may also be an absolute path.
http://www.cmake.org/cmake/help/v3.0/command/add_subdirectory.html
It does exactly what you need.