vincenzopalazzo
vincenzopalazzo

Reputation: 1645

CMake Cannot specify include directories when use target_include_directories

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

Upvotes: 18

Views: 43845

Answers (2)

Cloud Cho
Cloud Cho

Reputation: 1774

I had the same error but I had different command lines. For my case, I had "add_library" instead of "find_library" in the Vince's "CMakeLists.txt". In my situation, I solved by putting "add_library" before "target_include_directories" like this:

add_library( ${target_lib} STATIC
   ${CMAKE_CURRENT_SOURCE_DIR}/include/Buffer.cpp )

# set_target_properties( ${target_lib} PROPERTIES LINKER_LANGUAGE CXX )

# This will change folder location at "#include" in C++ code
target_include_directories( ${target_lib} PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/include/ )
target_sources( ${target_lib} PUBLIC 
  ${CMAKE_CURRENT_SOURCE_DIR}/src )

Here is my entire CMakeLists.txt as reference:

# CMakeLists.txt
# 
# Reference:
#   HPP and CPP in different folder
#     https://stackoverflow.com/a/71691919/5595995
#   Two library directories
#     https://stackoverflow.com/q/51960885/5595995

# ----- 1. Runtime requirement 

cmake_minimum_required(VERSION 3.14.0)
set(CMAKE_CXX_STANDARD 11)

# ----- 2. Debugging

# ----- 3. Global variable

set(project_name Simple_Task)
project(${project_name})

set( includes ${CMAKE_CURRENT_SOURCE_DIR}/include )

# ----- 4. Software tool

# ----- 5. Library

include_directories( ${includes} ) 

set( sources ${CMAKE_CURRENT_SOURCE_DIR}/src/test_simple.cpp )
set( target_lib ${CMAKE_PROJECT_NAME}-lib )

add_library( ${target_lib} STATIC
   ${CMAKE_CURRENT_SOURCE_DIR}/include/Buffer.cpp )

# This will change folder location at #include in C++ code
target_include_directories( ${target_lib} PUBLIC
  ${CMAKE_CURRENT_SOURCE_DIR}/include )
target_sources( ${target_lib} PUBLIC 
  ${CMAKE_CURRENT_SOURCE_DIR}/src )

# ----- 6. Compile

add_executable( ${project_name} ${sources} )
target_link_libraries( ${project_name} PUBLIC ${target_lib} ) 
target_include_directories( ${project_name} PUBLIC ${includes} ) 

My "CMakeLists.txt" is mixed with "include_directories" (older method) and "target_include_directories" (newer way), so it may not a good example. Please, consider visit other web page like Victor's suggestion for better guide.

Upvotes: 0

vincenzopalazzo
vincenzopalazzo

Reputation: 1645

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>.

Upvotes: 26

Related Questions