I have this directory structure:
projects/
project1/
src/
main.cpp
CMakeLists.txt
project2/
src/
file1.h
file1.cpp
test1.cpp
The top level projects directory cannot be considered a top level project, but just a collection of unrelated projects, so I would not put a CMakeLists.txt file at that level.
I want project1 to include files from project2 without specifying the full path, e.g.:
// main.cpp
#include "file1.h"
And I also want implementation files of project2 to be built in project1.
I need project2 not to be a library, but just use its files like if they were part of project1.
I am using cmake, and this is the CMakeLists.txt file I wrote (it does not work):
cmake_minimum_required(VERSION 2.8)
project(project1)
add_subdirectory(src)
add_subdirectory(../../project2/src)
Even specifying the full path to project2 does not work: ${CMAKE_CURRENT_SOURCE_DIR)/../../project2/src
I get "fatal error: file1.h: no such file or directory" from make.
To use external include files, this works:
include_directories
Related
I am attempting to integrate a C++ library I downloaded into a new project using CMake.
I downloaded the library ("downloaded_library"), created the build folder, and inside of it ran cmake .. and make. These both ran successfully, and than I navigated to the build folder and ran ./example to run the example file that came with the library. This also was successful, and so I hoped to add it to another project.
I added this working project into the 'libraries' folder of the following directory structure:
project
-libraries
-downloaded_library
-build
-include
-downloaded_lib
-downloaded_lib.h
-src
-examples
-example.cpp
-CMakeLists.txt
-src
-main.cpp
-build
-CMakeLists.txt
I hope to run the same code that ran in example.cpp in main.cpp, but I have been unable to get the import working. I navigate to the build folder and cmake .. runs successfully, but 'make' fails with an import error (fatal error on the include line, can't find the header file). This makes sense, as I didn't expect the same include line to work (I copy and pasted example.cpp to main.cpp), but I'm not sure what to make that.
fatal error: downloaded_lib/downloaded_lib.h: No such file or directory
1 | #include "downloaded_lib/downloaded_lib.h"
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
In this situation, what should my CMakeLists.txt contain, and what should my #include contain to be able to use the contents of this library in main.cpp in the same way I can in example.cpp?
EDIT---
I changed the package names above for simplicity sake, but the repository of 'downloaded_library' is the following: https://github.com/siposcsaba89/socketcan-cpp.
My top level CMakeLists.txt looks like this:
# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)
# Name of the project
project(projectname)
# Add all the source files needed to build the executable
add_executable(${PROJECT_NAME} src/main.cpp)
add_subdirectory(libraries/downloaded_library)
# Link the executable and the library together
target_link_libraries(${PROJECT_NAME} downloaded_library)
Edit 2-- (Here I will use the original package name, socketcan-cpp).
Top level CMakeLists.txt:
# Minimum version of CMake required to build this project
cmake_minimum_required(VERSION 3.0)
# Name of the project
project(socketcan_demo)
# Add all the source files needed to build the executable
add_executable(${PROJECT_NAME} src/main.cpp)
add_subdirectory("libraries/socketcan-cpp")
target_include_directories(${PROJECT_NAME} PUBLIC "libraries/socketcan-cpp/include")
When running make I get this error:
fatal error: socketcan_cpp/socketcan_cpp_export.h: No such file or directory
4 | #include <socketcan_cpp/socketcan_cpp_export.h>
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
So the top level CMakeLists is able to find the header file located in include, but that header file contains the line: #include <socketcan_cpp/socketcan_cpp_export.h> which references a file in a gen directory inside the build directory created by the library's CMakeLists.txt (https://github.com/siposcsaba89/socketcan-cpp/blob/master/CMakeLists.txt). And my top level package is unable to find it.
In order to include the header files from the downloaded library, you can add the following to the CMakeLists.txt file:
find_library(downloaded_lib
NAMES downloaded_lib
HINTS "path to downloaded lib file")
if(NOT downloaded_lib)
message(FATAL_ERROR "downloaded_lib not found!")
endif()
target_include_directories(${PROJECT_EXECUTABLE} PUBLIC "path to download_lib.h")
target_link_libraries(${PROJECT_EXECUTABLE} ${downloaded_lib})
This includes the directory where the header file is located in addition to the library file.
For my research project I am setting up a project (coom) to benchmark a set of algorithms on a data structure. For unit testing I settled on Bandit, which leaves me with a project structure that looks as follows:
+ root
|-- CMakeLists.txt
|-+ external/
| \-- bandit/
|-+ src/
| |-- CMakeLists.txt
| |-- node.cpp
| \-- node.h
\-+ test/
|-- CMakeLists.txt
|-- test.cpp
\-- test_node.cpp
From my experience with other languages, this seems to me a standard project structure? The test/ folder contains unit tests for the logic in src/ and no dependencies are intermixed with the source and test code, but are instead in external/.
The testing files I want to look as follows (with irrelevant parts removed)
// test/test.cpp
#include <bandit/bandit.h>
(...)
#include "test_node.cpp"
int main(int argc, char* argv[]) {
(...)
}
// test/test_node.cpp
#include <coom/node.h>
(...)
But my problem is, that when I try to compile with cmake .. and the subsequent Makefile, they are unable to find the source code in src/ where I get the compiler error:
fatal error: coom/node.h: No such file or directory.
I would expect the test/CMakeLists.txt should look somewhat like the following:
# test/CMakeLists.txt
add_executable (test_unit test.cpp)
target_link_libraries(test_unit coom)
I cannot figure out how to setup the CMakeLists.txt and src/CMakeLists.txt to ensure I get the desired outcome above. Currently they look as follows:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.8)
project (coom VERSION 0.1)
# ============================================================================ #
# Dependencies
(...)
# ============================================================================ #
# COOM project
add_subdirectory (src)
add_subdirectory (test)
# src/CMakeLists.txt
# ============================================================================ #
# Link up files for the library
set(HEADERS
node.h
)
set(SOURCES
node.cpp
)
add_library(coom ${HEADERS} ${SOURCES})
I can see from other projects, that it is possible to link the src/ directory with some libname/ prefix, but I cannot discern from their CMakeLists.txt files what I am doing wrong. I have looked at writing a coom.pc.in file and providing an install-target, and tried to set_target_properties with either FOLDER coom or PREFIX coom, but neither worked. I can hack an include_directory(../src) into the test/CMakeLists.txt to be able to include the file via an #include <node.cpp>, but that screams I'm doing something inherently wrong.
I'm at this point very much pulling my hairs out, and the CMake documentation is of very little help to me.
Your coom target has no include directories defined. You can define the include directories to use for this target (with target_include_directories()), and propagate these include directories so they are visible to the consuming test_unit target (by using PUBLIC):
# src/CMakeLists.txt
# ============================================================================ #
# Link up files for the library
set(HEADERS
node.h
)
set(SOURCES
node.cpp
)
add_library(coom ${HEADERS} ${SOURCES})
target_include_directories(coom PUBLIC ${CMAKE_CURRENT_SOURCE_DIR})
Furthermore, the file path to the node.h header is coom/src/node.h, not coom/node.h. But, because you now have coom/src as an public include directory, you can use the following to include the node.h header in your test file:
#include <node.h>
So I want to include a global header file that is in a different folder. The code for the CMakeList.txt is below. In my .cpp when I include something from the local include folder it works, but not for the something that is in a different folder.
cmake_minimum_required(VERSION 2.8)
#Project name
project(Server-Client)
#Add all cpp files as source files
file(GLOB_RECURSE SOURCES "src/*.cpp")
#Build executable 'Server' with all files in SOURCES
add_executable(Server ${SOURCES})
#Include all files in include directory
include_directories("include")
target_include_directories(Server PUBLIC "../../GlobalFiles/include")
find_package(Threads REQUIRED)
#Build executable 'localization' with all files in SOURCES
target_link_libraries(Server ${CMAKE_THREAD_LIBS_INIT})
Don't use a relative paths, instead use CMAKE_CURRENT_SOURCE_DIR variable like this:
target_include_directories(Server PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/../../GlobalFiles/include")
Other than that, it might be a good idea to use a Macro to find the global header you are looking for.
Assume I have a project of the following directory structure:
myproject
├── .git [...]
├── CMakeLists.txt
└── src
├── CMakeLists.txt
├── foo.cc
└── foo.h
If in src/foo.cc I include the header file like #include "foo.h" and then run Google's cpplint.py on it, it complains with
src/foo.cc:8: Include the directory when naming .h files [build/include] [4]
So I include it as #include "./foo.h". Now I get another complaint:
src/foo.cc:8: src/foo.cc should include its header file src/foo.h [build/include] [5]
However, if I include it as #include "src/foo.h", the compiler won't find it, with my current setup of CMake. This is how my two CMakeLists.txt files look like:
CMakeLists.txt:
project(myproject)
add_subdirectory(src)
src/CMakeLists.txt:
set(SRCS foo.cc)
add_executable(foo ${SRCS})
Is the way I'm using CMake somehow fundamentally wrong? Should I remove the src/CMakeLists.txt file entirely, and specify all source files in the base CMakeLists.txt with their full path?
Or should I simply ignore cpplint's complaints, as they don't really fit to how CMake projects are to be set up?
Add include_directories(${CMAKE_SOURCE_DIR}) in your top level CMakeLists.txt, like Wander suggested:
project(myproject)
include_directories(${CMAKE_SOURCE_DIR})
add_subdirectory(src)
I have a C++ library organized like:
Library/
CMakeLists.txt
main.cpp
src/
file1.cpp
include/
file1.h
Domains/
CMakeLists.txt
BlackJack/
CMakeLists.txt
src/
foo1.cpp
include/
foo1.h
...
libs/
tapkee/
CMakeLists.txt
include/
foo.hpp
....
Each library(i.e. Domains) contains linking and simple code for organizational purposes:
project(Domains)
cmake_minimum_required(VERSION 2.8)
add_subdirectory(IDomain)
add_subdirectory(Blackjack)
add_library(Domains STATIC)
target_link_libraries(Domains IDomain BLACKJACK)
Each sub-library (i.e. Blackjack) has a really simple CMakeLists.txt that just compiles the library (add_library).
This has been working just fine, until now.
I'm adding a third-party library (http://tapkee.lisitsyn.me/). This is a header-only library. It looks like CMake doesn't support this, but I found some work-arounds. I create a empty dummy.cpp file. Here is the CMakeLists for the third-party library:
project (TAPKEE)
file(GLOB TAPKEE_INCLUDE
"include/tapkee/*.hpp"
"include/tapkee/callbacks/*.hpp"
"include/tapkee/defines/*.hpp"
"include/tapkee/external/*.hpp"
"include/tapkee/neighbors/*.hpp"
"include/tapkee/parameters/*.hpp"
"include/tapkee/routines/*.hpp"
"include/tapkee/traits/*.hpp"
"include/tapkee/utils/*.hpp"
)
add_library(${PROJECT_NAME} header_only_BS.cpp ${TAPKEE_INCLUDE})
Here is the weird thing: My main.cpp and anything in the root src directory can include this tapkee library just fine, but none of the sub-libraries can (i.e. BlackJack). I get thousands of redefinition linker errors for the functions within tapkee.
Here is an example of the top-level CMakeLists:
project(ManifoldLearning)
#Root
set(SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include)
#Domains
set(BLACKJACK_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/Domains/Blackjack/include)
set(TAPKEE_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/libs/tapkee/include/)
include_directories(${INCLUDE_DIR} ${SRC_DIR})
include_directories(${TAPKEE_INCLUDE_DIR})
include_directories(${BLACKJACK_INCLUDE_DIR})
add_subdirectory(Domains)
#add_subdirectory(libs/tapkee)
add_executable(${PROJECT_NAME} ${SRC_LIST} ${HEADER_LIST})
target_link_libraries(${PROJECT_NAME} Domains)
I've been stuck on this for days, and any help/advice would be appreciated.
Update: I've removed the inclusion of the tapkee CMakeLists.txt (add_subdirectory) as per Antonio's direction. I still get redefinition errors, which I find strange. It's now not compiled anywhere and only included in BlackJack/src/foo1.cpp