Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cmake Cannot specify include directories when use target target_include_directories

Tags:

c++

cmake

I'm using the submodule GitHub inside my project and now I want to use the target_include_directories for including the file inside the my project class

This is my cmake configuration

cmake_minimum_required(VERSION 3.9)
project(SpyCBlock)

set(CMAKE_CXX_STANDARD 14)

#bitcoin rpc lib
find_library(bitcoinapi 0.3 REQUIRED)

target_include_directories(rapidjson PUBLIC include/rapidjson/include)

target_include_directories(spycblockrpc PUBLIC include/spycblockrpc)

target_include_directories(btccryptography PUBLIC include/bitcoin-cryptography-library)

add_executable(

        ${PROJECT_NAME}

        #other inclusion file cpp

        #cpp-properties file include
        include/cpp-properties/src/Properties.cpp
        include/cpp-properties/src/PropertiesParser.cpp
        include/cpp-properties/src/PropertiesUtils.cpp

        #include bitcoin-cryptography-library
        include/bitcoin-cryptography-library/cpp/Sha256.cpp
        include/bitcoin-cryptography-library/cpp/Sha256Hash.cpp
        include/bitcoin-cryptography-library/cpp/Utils.cpp

        #include spycblocrpc
        include/spycblockrpc/core/graph/TransactionGraph.cpp
        include/spycblockrpc/core/graph/WrapperInformations.cpp
        include/spycblockrpc/ConfiguratorSingleton.cpp

        include/spycblockrpc/commands/DecodeScriptCommand.cpp
        include/spycblockrpc/commands/DecodeRawTransaction.cpp
        include/spycblockrpc/commands/HeightBlockchainCommand.cpp
        include/spycblockrpc/commands/DecodeBlockAtIndexCommand.cpp

)

#bitcoin rpc lib
target_link_libraries(SpyCBlockTests bitcoinapi)
target_link_libraries(${PROJECT_NAME} bitcoinapi)

When run CMake I have this error

Starting to parse CMake project.
CMake Error at CMakeLists.txt:20 (target_include_directories):
  Cannot specify include directories for target "rapidjson" which is not
  built by this project.


CMake Error at CMakeLists.txt:22 (target_include_directories):
  Cannot specify include directories for target "spycblockrpc" which is not
  built by this project.


CMake Error at CMakeLists.txt:24 (target_include_directories):
  Cannot specify include directories for target "btccryptography" which is
  not built by this project.


CMake Error at CMakeLists.txt:26 (target_compile_definitions):
  Cannot specify compile definitions for target "cppproperties" which is not
  built by this project.

I'm new with the C++ and the cmake and I can't understand what I'm wrong

like image 939
vincenzopalazzo Avatar asked Aug 27 '19 09:08

vincenzopalazzo


People also ask

What is the difference between include_directories and target_include_directories?

include_directories(x/y) affects directory scope. All targets in this CMakeList, as well as those in all subdirectories added after the point of its call, will have the path x/y added to their include path. target_include_directories(t x/y) has target scope—it adds x/y to the include path for target t .

How ADD includes in CMake?

To include headers in CMake targets, use the command target_include_directories(...) . Depending on the purpose of the included directories, you will need to define the scope specifier – either PUBLIC , PRIVATE or INTERFACE .

What are target directories?

Specifies include directories to use when compiling a given target. The named <target> must have been created by a command such as add_executable() or add_library() and must not be an ALIAS target. By using AFTER or BEFORE explicitly, you can select between appending and prepending, independent of the default.

What is a CMake target?

A CMake-based buildsystem is organized as a set of high-level logical targets. Each target corresponds to an executable or library, or is a custom target containing custom commands.


1 Answers

I want to add the solution to this problem, as suggested in the comments, the code has two problems:

target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include/rapidjson/include)
  1. The first argument of the target must be the name of the executable, so in this case, is SpyCBlock
  2. The second problem is the definition of the target before the declaration of the target, the target_include_directories(SpyCBlock ...) is defined before the add_executable(${PROJECT_NAME} ...)

A minimal correct example is:

add_executable(
    ${PROJECT_NAME}
        
    #other inclusion file cpp
        
    #cpp-properties file include
    include/cpp-properties/src/Properties.cpp
    include/cpp-properties/src/PropertiesParser.cpp
    include/cpp-properties/src/PropertiesUtils.cpp

    #include bitcoin-cryptography-library
    include/bitcoin-cryptography-library/cpp/Sha256.cpp
    include/bitcoin-cryptography-library/cpp/Sha256Hash.cpp
    include/bitcoin-cryptography-library/cpp/Utils.cpp

    #include spycblocrpc
    include/spycblockrpc/core/graph/TransactionGraph.cpp
    include/spycblockrpc/core/graph/WrapperInformations.cpp
    include/spycblockrpc/ConfiguratorSingleton.cpp

    include/spycblockrpc/commands/DecodeScriptCommand.cpp
    include/spycblockrpc/commands/DecodeRawTransaction.cpp
    include/spycblockrpc/commands/HeightBlockchainCommand.cpp
    include/spycblockrpc/commands/DecodeBlockAtIndexCommand.cpp
)
        
target_include_directories(${PROJECT_NAME} PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}>/include/rapidjson/include)

Now I can include the library like this <bitcoin-cryptography-library/Sha256.h>.

like image 179
vincenzopalazzo Avatar answered Sep 17 '22 19:09

vincenzopalazzo