I've got a question on cmake project configuration.
The following is my project structure.
trunk
|
|---- mylib
| |
| ---- mylib.h
| ---- mylib.c
| ---- *MYLIB_CMakeList.txt*
|
|---- alib
| |
| ---- alib.h
| ---- alibName.lib
| ---- *ALIB_CMakeList.txt*
|
|
|---- main
| |
| ---- main.cpp
| ---- *MAIN_CMakeList.txt*
|
---- *TOP_CMakeList.txt*
mylib subfolder contains the source code (c) to create my own static library. This code depends on alib.
The main subfolder contains the example code (C++) which uses mylib.
Here is how I've written my CMakeList files:
**TOP_CMakeList.txt**
cmake_minimum_required(VERSION 3.4)
add_subdirectory(mylib)
add_subdirectory(alib)
add_subdirectory(main)
------------------------------------------------------------------------------------------------------------
**MYLIB_CMakeList.txt**
cmake_minimum_required(VERSION 3.4)
project(MyLib C)
if (WIN32)
set(CMAKE_SHARED_LIBRARY_PREFIX "")
endif ()
include_directories(${alib_INCLUDE_DIRS})
add_library(${PROJECT_NAME} STATIC mylib.c)
target_link_libraries(${PROJECT_NAME} alib)
set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE)
set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
------------------------------------------------------------------------------------------------------------
**ALIB_CMakeList.txt**
cmake_minimum_required(VERSION 3.4)
project(ALib)
set(${PROJECT_NAME}_DEFINITIONS CACHE INTERNAL "${PROJECT_NAME}: Definitions" FORCE)
set(${PROJECT_NAME}_INCLUDE_DIRS ${PROJECT_SOURCE_DIR} CACHE INTERNAL "${PROJECT_NAME}: Include Directories" FORCE)
------------------------------------------------------------------------------------------------------------
**MAIN_CMakeList**
make_minimum_required(VERSION 3.4)
project(Executable CPP)
file(GLOB CPP_FILES *.cpp)
add_definitions(${MyLib_DEFINITIONS})
include_directories(${MyLib_INCLUDE_DIRS})
add_executable(${PROJECT_NAME} ${CPP_FILES})
target_link_libraries(${PROJECT_NAME} MyLib)
With such configuration it doesn't recognize the alib dependency of mylib. In particular (I'm developing under Windows) I get this error:
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../x86_64-pc-cygwin/bin/ld:
cannot find -lalibName.lib
I can't figure out how to correctly do it. Any suggestions?
Seems that the alib project does not specify any kind of library targets the same way as mylib does:
Try adding this to alibs' CMake file
add_library(${PROJECT_NAME} STATIC <<< alib SOURCES HERE >>>)
Static libraries can't have dependencies(well, they can but they don't drag them with themselves) so there is no point in providing alib in target_link_libraries(...) for another static library. You should provide alib to the add_execuable command directly.
Alternatively, you can build a big static library from both mylib and alib but it will be less elegant and platfrom dependant. For gcc you should look at ar tool.
Remove the add_subdirectory(alib) command, then remove CMake file from the alib subfolder — you don't need it. Then in the cmake file where you have your add_executable command put the following(after the line);
link_directories(../alib)
target_link_libraries(${PROJECT_NAME} alibName.lib)
Related
I have multiple projects (subdirectories) inside my repository. All projects have only one executable file named main.cpp and they all use libraries from the common folder with #include statements. The folder structure looks like this:
root
|
├────common
| ├──── example.h
| ├──── example.cpp
| ├──── *.h # many header files
| └──── *.cpp # many source files
|
├────Project_A
| ├──── CMakeLists.txt
| └──── main.cpp
|
├────Project_B
| ├──── CMakeLists.txt
| └──── main.cpp
|
└──── CMakeLists.txt
This is my attempt at writing the root's CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
project ("root")
file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
link_libraries(${CommonLibs})
add_subdirectory ("Project_A")
add_subdirectory ("Project_B")
Project_A's CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
add_executable (Project_A "main.cpp")
Project_B's CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
add_executable (Project_B "main.cpp")
However when running any of the projects I get this error:
LNK1107 invalid or corrupt file: cannot read at 0x7AC, file example.h
I don't believe the file is corrupted because before I tried to use link_libraries() in root's CMakeLists.txt I was getting other error:
Unresolved external symbol SomeNamespace::ExampleClass::ExampleClass(bool)
Possible duplicates
Other questions such as this one don't solve my problem because they usually work with a more complex folder structure. Also there are questions that attempt to target only single project like this one, but I have multiple projects.
Can you please provide a concise solution?
Add a CMakeLists.txt to your common/ directory:
root
|
├────common
| ├──── CMakeLists.txt <-------- Add this
| ├──── example.h
....
common/CMakeLists.txt
file(GLOB_RECURSE CommonLibsFiles ${CMAKE_CURRENT_SRC_DIR}/*.cpp)
add_library(CommonLibs ${CommonLibsFiles})
...
root's CMakeLists.txt
...
### NO NEED FOR THESE
file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
link_libraries(${CommonLibs})
###
add_subdirectory(common) #CommonLibs will now be visible to children directories
...
Now link the libraries as required. For example for project A:
Project_A's CMakeLists.txt
cmake_minimum_required (VERSION 3.8)
add_executable (Project_A "main.cpp")
target_link_libraries(Project_A PRIVATE CommonLibs) #link to common libs here,
All the targets/variables that are created or are visible to parent cmake file, are visible to children. The reverse is not true though. For children to expose their variables to parent, they need to explicitly specify PARENT_SCOPE
The following lines in your top-level CMake do not make much sense:
file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
link_libraries(${CommonLibs})
The link_libraries() command accepts libraries or library targets, not variables (such as CommonLibs). You can use the CommonLibs variable to define a new library target, then link the library target to your executables using link_libraries():
file(GLOB_RECURSE CommonLibs ${CMAKE_SOURCE_DIR}/common/*.cpp)
# Add this line.
add_library(MyCommonLib SHARED ${CommonLibs})
link_libraries(MyCommonLib)
Please note that the use of link_libraries() in general is discouraged, even in CMake documentation itself. You should prefer linking your MyCommonLib to the specific executables that require it using target_link_libraries.
target_link_libraries(Project_A PRIVATE MyCommonLib)
There are numerous similar questions on Stack Overflow, but none come near to answering my question.
I have a C++ library built using CMake:
mylib
| - CMakeLists.txt
| - src/
| | - m.h
| | - m.cpp
| - include/
| | - mylib/
| | | - a.h
| | | - something/
| | | | - some.h
| - cmake/
| - mylibConfig.cmake.in
| - mylibConfigVersion.cmake.in
I then create another library or executable which includes the aforementioned library:
myapp
| - CMakeLists.txt
| - src/
| | - main.cpp
| - include/
| | - myapp/
| | | - b.h
| - libs
| | - mylib
And would like to use mylib within myapp like so. Please take notice how mylib headers are included in a directory like format:
#include <mylib/a.h>
#include <mylib/something/some.h>
mylib should be built when building myapp so that the following works without any other build steps:
$ cd myapp/build
$ cmake ..
$ make
Here is a list of some of the Stack Overflow posts I have reviewed. I have attempted each, and they simply do not work:
CMake: How to build external projects and include their targets
CMake ExternalProject_Add() and FindPackage()
Assuming in the project described, only header files in the mylib/include directory are to be considered public:
The top-level CMakeLists.txt for mylib needs to contain:
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(mylib VERSION 0.1 LANGUAGES CXX)
add_library(${MyLibName} ${MyLib_SOURCES})
target_include_directories(${MyLibName}
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src
)
This ensures that projects including mylib will only have access to files in the include/ directory. Files in include/ can be used like #include <myfile.h> and files in include/mylib (the general convention) can be used like #include <mylib/myfile.h>.
The top-level CMakeLists.txt for myapp should include:
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(myapp VERSION 0.1 LANGUAGES CXX)
add_subdirectory(libs/mylib)
add_executable(${MyAppName} ${MyApp_SOURCES})
target_link_libraries(${MyAppName} ${MyLibName}
The use of add_subdirectory ensures that mylib will be built before myapp and target_link_libraries adds mylib to the executable.
As mentioned by Tzalumen, make sure you take a look at the CMake tutorials and prefer the use of cmake --build . instead of make.
So, I actually do this in one of my projects.
First, the library needs to be built with ADD_LIBRARY(), but I'm assuming you do that. Something like
ADD_LIBRARY (${MyLibName} ${SOURCES}) in the library's CMakeLists.txt
That adds the library into cmake's list of libraries.
Second, you add the library to your project with
target_link_libraries (${PROJECT_NAME} ${MyLibName}) in the executable's CMakeLists.txt, the same one where you have your add_executable (${PROJECT_NAME} ${SOURCES})
That will set up the dependency chain to force MyLibName to explicitly build before PROJECT_NAME.
I have a C++ project containing several modules, some built as libraries, with such structure:
/MyProject
+---/build
/ModuleA
+---CMakeLists.txt <- module level CMakeLists
+---/src
| +--CMakeLists.txt <- src level CMakeLists
| +--FileA1.cpp
| +--FileA2.cpp
+---/include
| +--FileA1.h
| +--FileA2.h
| +--FileA3.h
/ModuleB
+---CMakeLists.txt
+---/src
| +--CMakeLists.txt
| +--FileB1.cpp
| +--FileB2.cpp
+---/include
| +--FileB1.h
| +--FileB2.h
| +--FileB3.h
main.cpp
CMakeLists.txt <- project level CMakeLists
CMakeLists.txt files look as follow:
project level:
cmake_minimum_required(VERSION 3.05)
project(MyProject)
subdirs(ModuleA ModuleB)
set(CMAKE_CXX_STANDARD 11)
add_executable(MyProject main.cpp)
target_link_libraries(MyProject ModuleA ModuleB)
module level:
subdirs(src)
src level:
FIND_PACKAGE(SomePackage REQUIRED)
INCLUDE_DIRECTORIES(
${SomePackage_INCLUDE_DIR}
${MyProject_SOURCE_DIR}/ModuleA/include
)
SET(SOURCE_FILES <all files from ModuleA/src goes here>)
ADD_LIBRARY(ModuleA STATIC ${SOURCE_FILES})
TARGET_LINK_LIBRARIES(ModuleA
${SomePackage_LIBRARIES}
)
The problem is: when I include header files from 'SomePackage' in my ModuleA header files (i.e. SomePackageFile.hpp in FileA1.h) I get an error while running a build with make:
fatal error: SomePackageFile.hpp: No such file or directory
When I include them in cpp files they are visible and project compiles correctly. I assume that is sth wrong with CMakeLists on src level or entire hierarchy of files is missing something.
I have a github project to be used as a skeleton for other projects:
https://github.com/gnyiri/cmake-sandbox
If you follow this layout, you will not need to add ${SomePackage_INCLUDE_DIR} to INCLUDE_DIRECTORIES which is not the best way to add directories to the include path otherwise.
In a nutshell, you should define a new library like this:
project(module_a)
set(sources
src/source_a_1.cc
)
add_library(library_a
${sources}
)
target_include_directories(library_a
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
Then, if you define another library (library_b), you only need to add library_a in target_link_libraries:
project(module_b)
# set list of sources, needs to be extended when new source arrives
set(sources
src/source_b_1.cc
)
# define a library (static by default -> liblibrary_b.a or library_a.lib will be generated)
add_library(library_b
${sources}
)
# include directories
target_include_directories(library_b
PUBLIC
$<INSTALL_INTERFACE:include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
)
# link library_b
target_link_libraries(library_b
library_a
)
Note that in this source tree, all the header files are located in
<module>/include/<module>
This way you will include a header file from like this:
#include "<module>/<module_header.h>"
This is simply because /include will be on include path.
Switching from INCLUDE_DIRECTORIES() to TARGET_INCLUDE_DIRECTORIES() was the case, no change to the structure of the project was needed.
Having problems setting up a project to keep directory structure with cmake.
Project structure I want to keep:
Project
|-libA
| |-fileA.h
| |-fileA.cpp
| |-CMakeLists.txt
|-include
| |-fileI.h
|-program1
| |-main.cpp
| |-CMakeLists.txt
|-CMakeLists.txt
source code include example:
#include "libA/fileA.h"
This doesn't work. Cmake can't find "libA/fileA.h", it will only work if I create a following directory structure:
Project
|-libA
| |-libA (directory)
| |-fileA.h
| |-fileA.cpp
| |-CMakeLists.txt
|-include
| |-fileI.h
|-program1
| |-main.cpp
| |-CMakeLists.txt
|-CMakeLists.txt
What can I do to keep the project structure as shown in the first example and to keep directory name in source files? (eg #include "libA/fileA.h")
Project cmake:
cmake_minimum_required(VERSION 3.8)
project(A)
include_directories( ${PROJECT_SOURCE_DIR}/include )
add_subdirectory(libA)
add_subdirectory(program1)
libA cmake:
set(LIBA_SRC libA/fileA.cpp)
add_library(libA SHARED ${LIBA_SRC})
Program1 cmake:
set (PROGRAM1_SRC main.cpp)
add_executable (program1 ${PROGRAM_SRC})
target_link_libraries (program1 libA)
target_include_directories (program1 PUBLIC ${PROJECT_SOURCE_DIR}/libA)
If you change the include directory in program1 to
target_include_directories (program1 PUBLIC ${PROJECT_SOURCE_DIR})
it will search from your source directory, so "libA/fileA.h" will find it's target.
Well, I am new to CMake.
I have the following file structure
haze_removal
|---build
|---bin
| |--Test
| |--CMakeLists.txt
|---lib
| |--libtools.so
|---include
| |--tools.hpp
|---test
| |--main.cpp
| |--CMakeLists.txt
|---src
| |--tools.cpp
| |--CMakeLists.txt
|---CMakeLists.txt
The libtools.so is builded from ../src/tools.cpp. I build the whole project in ../build using the following cmake command:
cmake -DCMAKE_BUILD_TYPE=Debug ..
make
The Test is build from ../test/main.cpp
I build this project successfully. But when I debug Test using gdb ./Test, I can't skip in the function that from libtools.so.
These are my CMakeLists.txt from different directories.
CMakeLists.txt under haze_removal/
cmake_minimum_required(VERSION 2.8)
project(haze_removal)
# find needed package
find_package(OpenCV REQUIRED)
# library directory
add_subdirectory(src)
# test
add_subdirectory(test)
CMakeLists.txt under ../src/
# generate dynamic library
# add source file, include directories
aux_source_directory(. TOOLS_SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
# generate
add_library(tools SHARED ${TOOLS_SRC})
# set output directory and lib's name
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)
set_target_properties(tools PROPERTIES OUTPUT_NAME "tools")
# link library
target_link_libraries(tools ${OpenCV_LIBS})
CMakeLists.txt under ../test/
# add source file, include directories, link directories
aux_source_directory(. EXE_SRC)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
# generate
add_executable(Test ${EXE_SRC})
# set output directory
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
# link libraries
target_link_libraries(Test ${OpenCV_LIBS} libtools.so)
My question is how I can debug the functions that from libtools.
Well, I find that why I can't step in the dynamic library even though I set CMAKE_BUILD_TYPE=Debug. Because before I set the build model to Debug I used to set CMAKE_BUILD_TYPE=Release. After I change the build model, I didn't delete the files in the build directory.
I don't know whether the above explanation is right or not, but I do solve my problem.
Thx!