I am trying to add jwt-cpp library by Thalhammer in my project using CMake.
My project structure is as follows
build
External
---- jwt
.gitignore
.gitmodules
CMakeLists.txt
main.cpp
I used command git submodule add https://github.com/Thalhammer/jwt-cpp.git External/jwt to add the mentioned git submodule to my project. and the contents of the .gitmodules file is
[submodule "External/jwt"]
path = External/jwt
url = https://github.com/Thalhammer/jwt-cpp.git
My CMakeLists.txt looks like
cmake_minimum_required(VERSION 3.24.2)
set(This Trial)
project(${This} C CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(Sources main.cpp)
add_executable(${This} ${Sources})
add_subdirectory(External/jwt)
if(NOT TARGET jwt-cpp)
find_package(jwt-cpp CONFIG REQUIRED)
endif()
target_include_directories(${This} INTERFACE External/jwt/include)
target_link_libraries(${This} INTERFACE jwt-cpp::jwt-cpp)
and my main.cpp is
#include <iostream>
#include <jwt-cpp/jwt.h>
int main ()
{
return 0;
}
The issue I am facing is that everything configures builds properly if is don't include #include <jwt-cpp/jwt.h> in my main.cpp file but throws and error if I do that. The error looks like as follows:
fatal error: 'jwt-cpp/jwt.h' file not found
#include <jwt-cpp/jwt.h>
^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/Trial.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Trial.dir/all] Error 2
make: *** [all] Error 2
I am not able to figure out what should I correct/edit.
The INTERFACE property adds folders for dependent targets. You need to use the PUBLIC property so that folders are added to the target to be builded.
Related
I am trying to link the following library : nngpp
using the following commands
mkdir build
cd build
cmake ..
make
make install
However when testing the demos or using the library in a project with the following in CMakeLists.txt :
...
add_executable(target main.cpp)
target_link_libraries(target nngpp)
I get the following error:
fatal error: 'nngpp/nngpp.h' file not found
#include <nngpp/nngpp.h>
^~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/rest.dir/rest/server.o] Error 1
make[1]: *** [CMakeFiles/rest.dir/all] Error 2
make: *** [all] Error 2
note : The library is header-only. but I don't want to copy it in my project.
With
find_package(nngpp)
using the library is straightforward:
add_executable(target main.cpp)
target_link_libraries(target nng::nngpp)
Here nng::nngpp is IMPORTED library, so it cares about include directories for you.
It's hard to know exactly what's wrong without more information. I would suggest running make VERBOSE=1. This will show you the command line that is being executed when trying to compile the file that gives you the error.
Look in the command line for include flags (e.g. -I flags if you're using gcc). Find the directory for nngpp and double-check if your header files are in there, and what the correct path relative to that directory is to reference the header file. Maybe you need to only #include <nngpp.h> instead?
The following worked :
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)
find_package(nng REQUIRED)
find_package(Threads)
find_package(nngpp REQUIRED)
add_executable(server src/tfo-server.cpp)
target_link_libraries(server nng::nng nng::nngpp)
This also worked:
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(comm)
set(CMAKE_CXX_STANDARD 11)
add_executable(server src/tfo-server.cpp)
include_directories(server /usr/local/include)
We (three students) are very new to CMake. For a university project we have to include GoogleTest into our codebase, and we are having a lot of trouble with it.
We have a gtest_ours.cmake file:
project("googletests")
enable_testing()
include(GoogleTest)
file(GLOB_RECURSE MY_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
# header don't need to be included but this might be necessary for some IDEs
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
EXCLUDE
"${CMAKE_CURRENT_SOURCE_DIR}/src/MolSim.cpp"
)
add_subdirectory(googletest)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})
add_executable(ParticleContainerTest ${sources} ${CMAKE_CURRENT_SOURCE_DIR}/test/ParticleContainerTest.cpp )
target_link_libraries(ParticleContainerTest ${MY_SRC} gtest gtest_main gmock gmock_main)
gtest_discover_tests(ParticleContainerTest)
set_tests_properties(${noArgsTests} PROPERTIES TIMEOUT 10)
set_tests_properties(${withArgsTests} PROPERTIES TIMEOUT 20)
and from the CMakeLists.txt file, we just call:
include(gtest_ours)
Our folder structure is:
main
- src
- test
- CMakeLists.txt
- others
Please help, we get the following error:
/home/lunaticcoding/psemoldyn_groupc/MolSim-master/test/ParticleContainerTest.cpp:9:10: fatal error: ../src/ParticleContainer.h: No such file or directory
#include "../src/ParticleContainer.h"
^~~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/ParticleContainerTest.dir/build.make:62: recipe for target 'CMakeFiles/ParticleContainerTest.dir/test/ParticleContainerTest.cpp.o' failed
make[2]: *** [CMakeFiles/ParticleContainerTest.dir/test/ParticleContainerTest.cpp.o] Error 1
CMakeFiles/Makefile2:144: recipe for target 'CMakeFiles/ParticleContainerTest.dir/all' failed
make[1]: *** [CMakeFiles/ParticleContainerTest.dir/all] Error 2
Makefile:140: recipe for target 'all' failed
make: *** [all] Error 2
There are a couple issues with the gtest_ours.cmake file. Your error is raised because you haven't defined the include directories for your ParticleContainerTest target to include ParticleContainer.h. You've only added gtest_SOURCE_DIR directories as include directories. To correct this, consider adding this line after add_executable():
target_include_directories(ParticleContainerTest PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src)
This way, the src directory will be an include directory, so you can modify test/ParticleContainerTest.cpp to have a more reasonable #include:
#include "ParticleContainer.h"
Also, I'm not sure where you intend to use the ${MY_SRC} source files, but you don't want to include this in the target_link_libraries() call. This call should only include pre-defined targets, library names, or linking options as arguments; do not add a list of source files to this call. I'm also not sure where you defined the ${sources} argument in the add_executable() call, but perhaps, you meant to put ${MY_SRC} there instead.
Finally, the EXCLUDE qualifier does not work with the file(GLOB_RECURSE ...) signature. However, you can remove a specific file from the list afterward, using list(REMOVE_ITEM ...):
file(GLOB_RECURSE MY_SRC CONFIGURE_DEPENDS
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.cpp"
# header don't need to be included but this might be necessary for some IDEs
"${CMAKE_CURRENT_SOURCE_DIR}/src/*.h"
)
list(REMOVE_ITEM MY_SRC ${CMAKE_CURRENT_SOURCE_DIR}/src/MolSim.cpp)
I'm using external files for work with my library, so but I do not want using the relative path inside my file C++ but I want using this convention
I read that with CMake is possible to create this if using the target_include_directories
I'm new with Cmake and I have a problem with configuring this target on my project
This is my directory configuration
This is my CMake configuration
cmake_minimum_required(VERSION 2.6)
project(decompile-bitcoin-script)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES
main.cpp
#Bitcoin Lib
bitcoinlib/script.cpp
bitcoinlib/script_error.cpp
bitcoinlib/key_io.cpp
bitcoinlib/pubkey.cpp
bitcoinlib/sign.cpp
bitcoinlib/standard.cpp
)
add_executable(Decompiler ${SOURCE_FILES})
target_include_directories(Decompiler PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/bitcoinlib)
This is the compiler error
[ 12%] Building CXX object CMakeFiles/Decompiler.dir/main.cpp.o
[ 25%] Building CXX object CMakeFiles/Decompiler.dir/bitcoinlib/script.cpp.o
/home/vincenzo/Github/decompiler-bitcoin-script/bitcoinlib/script.cpp:6:10: fatal error: bitcoinlib/script.h: No such file or directory
#include <bitcoinlib/script.h>
^~~~~~~~~~~~~~~~~~~~~
compilation terminated.
CMakeFiles/Decompiler.dir/build.make:86: recipe for target 'CMakeFiles/Decompiler.dir/bitcoinlib/script.cpp.o' failed
make[2]: *** [CMakeFiles/Decompiler.dir/bitcoinlib/script.cpp.o] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/Decompiler.dir/all' failed
make[1]: *** [CMakeFiles/Decompiler.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Inside the main not have code but I have only main with a cout<<"foo";
What I'm doing wrong?
target_include_directories(Decompiler PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/bitcoinlib)
This line is telling the compiler that ./bitcoinlib is the root of the include path.
So #include <bitcoinlib/script.h> is looking for ./bitcoinlib/bitcoinlib/script.h.
You seem to have set up your project directory with CMakeLists.txt inside your source/headers directory, so you can change the target_include_directories setting like this:
target_include_directories(Decompiler PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>)
Or, you can change ./bitcoinlib/script.cpp to #include <script.h> since that header is in the same directory as the .cpp file anyways.
I would recommend that you restructure your project a bit though:
<Project Directory>
CMakeLists.txt
include
bitcoinlib
< your bitcoinlib headers here >
src
bitcoinlib
< your bitcoinlib sources here >
main.cpp
target_include_directories(Decompiler PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>)
I need to use OpenSSL and cpprestsdk in a C++ project I'm working on but I'm having issues getting it to build properly. In my CMakeLists.txt I have:
cmake_minimum_required(VERSION 3.12)
project(Final_Project)
set(CMAKE_CXX_STANDARD 11)
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/bin/openssl)
find_package(cpprestsdk REQUIRED NAMES cpprestsdk cpprest )
find_package(OpenSSL REQUIRED)
add_executable(Final_Project main.cpp)
Which builds just fine and returns no errors. All my code as of right now is contained in main.cpp.
In main.cpp I have:
#include <iostream>
#include <string>
#include <cpprest/http_client.h>
#include <cpprest/filestream.h>
using namespace std;
int main(int argc, char* argv[])
{
cout << "Running!" << endl;
return 0;
}
But whenever I try to build/run it I get an error with cpprest:
Scanning dependencies of target Final_Project
[ 50%] Building CXX object CMakeFiles/Final_Project.dir/main.cpp.o
/Users/myAccount/myFolder/Final Project/main.cpp:3:10: fatal error: 'cpprest/http_client.h' file not found
#include <cpprest/http_client.h>
^~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
make[2]: *** [CMakeFiles/Final_Project.dir/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Final_Project.dir/all] Error 2
make: *** [all] Error 2
I installed OpenSSL and cpprestsdk with homebrew just fine and I've added everything I think I need to my PATHs. I've tried adding command line arguments to GCC to include the cpprestsdk path and I've tried renaming the #include <cpprest...> to #include <cpprestsdk...> but to no avail. Anyone have any ideas? I'm sure it's just something simple I'm missing.
I ended up figuring it out, my project name had a space in it so I refactored it without that and updated my CMake accordingly. I also had to add the target_link_libraries below the add_executable line. Example of my working CMakeLists below:
cmake_minimum_required(VERSION 3.12)
project(FinalProject)
set(CMAKE_CXX_STANDARD 11)
# Set OpenSSL dir, this *should* be default on linux/mac
set(OPENSSL_INCLUDE_DIR /usr/local/opt/openssl/bin/openssl)
# Get OpenSSL
find_package(OpenSSL REQUIRED)
# Get cppRestSDK
find_package(cpprestsdk REQUIRED)
# Compile + Link
add_executable(FinalProject main.cpp)
target_link_libraries(FinalProject PRIVATE cpprestsdk::cpprest)
While changing to a CMake and Travis CI build environment I enabled the compilation on Travis CI. Even though the CMake project compiles correctly on my pc, Travis exited with 2:
In file included from /home/travis/build/Codestones/Proton/source/application.cpp:1:0:
/home/travis/build/Codestones/Proton/source/application.h:3:23: fatal error: glad\glad.h: No such file or directory
#include <glad\glad.h>
^
compilation terminated.
make[2]: *** [CMakeFiles/Proton.dir/source/application.cpp.o] Error 1
make[1]: *** [CMakeFiles/Proton.dir/all] Error 2
make: *** [all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.1)
project(Proton)
set(CMAKE_CXX_STANDARD 11)
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
set(SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/source")
set(LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/dependencies")
# Executable definition and properties
file(GLOB SOURCES
"${SRC_DIR}/*.h"
"${SRC_DIR}/*.cpp"
)
add_executable(Proton "${SOURCES}")
# GLFW
set(GLFW_DIR "${LIB_DIR}/glfw")
set(GLFW_BUILD_EXAMPLES OFF CACHE INTERNAL "Build the GLFW example programs")
set(GLFW_BUILD_TESTS OFF CACHE INTERNAL "Build the GLFW test programs")
set(GLFW_BUILD_DOCS OFF CACHE INTERNAL "Build the GLFW documentation")
set(GLFW_INSTALL OFF CACHE INTERNAL "Generate installation target")
add_subdirectory("${GLFW_DIR}")
target_link_libraries(${PROJECT_NAME} "glfw" "${GLFW_LIBRARIES}")
target_include_directories(${PROJECT_NAME} PRIVATE "${GLFW_DIR}/include")
target_compile_definitions(${PROJECT_NAME} PRIVATE "GLFW_INCLUDE_NONE")
# GLAD
set(GLAD_DIR "${LIB_DIR}/glad")
add_subdirectory("${GLAD_DIR}")
target_link_libraries(${PROJECT_NAME} "glad" "${GLAD_LIBRARIES}")
target_include_directories(${PROJECT_NAME} PRIVATE "${PROJECT_BINARY_DIR}/dependencies/glad/include")
I assume this means that glad.h was missing or linked incorrectly, but why would it build and run fine on my pc?
\ cannot be used as path separator in #include directives, use /. If your local (Windows?) compiler understands it, it's a non-portable extension.
gcc supports backslashes in include paths on Windows as an extension
#include <glad\glad.h>
This is not supported under linux and you should only use forward slashes / in include paths
#include <glad/glad.h>