c++windowscmakecmakelists-optionscmake-language

CMake Error: Target of type EXECUTABLE may not be linked into another target


I try to add GTest project to my solution. I have a project structure: my project structure I created Cryptograph and CryptographTests directories, after that created binTests and lib into CryptographTests. I have a few CMakeLists.txt files:

cmake_minimum_required(VERSION 3.17)
project(Cryptograph)

set(CMAKE_CXX_STANDARD 17)

find_package(OpenSSL REQUIRED)

add_executable(Cryptograph main.cpp modulArithmetics.cpp modulArithmetics.h Speakers.cpp Speakers.h Crypt.cpp Crypt.h LongArithmetic.cpp LongArithmetic.h Signs.cpp Signs.h)
target_link_libraries(Cryptograph OpenSSL::SSL)
project(CryptographTest)

add_subdirectory(lib/googletest)
add_subdirectory(binTests)

project(CryptographGTest)

add_subdirectory(lib)
include_directories(${gtest_SOURCE_DIR}/include ${gtest_SOURCE_DIR})

add_executable(runCommonTests FirstTest.cpp)

target_link_libraries(runCommonTests gtest gtest_main)
target_link_libraries(runCommonTests Cryptograph)

cmake_minimum_required(VERSION 3.17)
project(CryptographGlobal)

set(CMAKE_CXX_STANDARD 17)

set (SOURCE_FILES main.cpp)
add_executable(cryptograph_samples ${SOURCE_FILES})

include_directories(Cryptograph)

add_subdirectory(Cryptograph)
add_subdirectory(CryptographTests)

target_link_libraries(cryptograph_samples Cryptograph)

After that i got errors:

CMake Error at CryptographTests/binTests/CMakeLists.txt:6 (target_link_libraries):
  Target "Cryptograph" of type EXECUTABLE may not be linked into another
  target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
  libraries, or to executables with the ENABLE_EXPORTS property set.


CMake Error at CMakeLists.txt:14 (target_link_libraries):
  Target "Cryptograph" of type EXECUTABLE may not be linked into another
  target.  One may link only to INTERFACE, OBJECT, STATIC or SHARED
  libraries, or to executables with the ENABLE_EXPORTS property set.

Before this error I got error lool like can't connect to Cryptograph.lib, but after my changes errors also changed.

I try to add GTest project to my solution, but got the error.


Solution

  • Your intuition that your test executable needs access to your Cryptograph code somehow in order to test it is correct. However, linking an executable to another executable is not possible.

    You'll want to make Cryptograph a library instead so that it compiles once and your executables (cryptograph_samples and runCommonTests) can link to it.

    The library should also not have a main(). Otherwise it would clash with your executable's main() at link-time. So assuming Cryptograph/main.cpp contains a main() function, it should be excluded.

    So, replace this line:

    add_executable(Cryptograph main.cpp modulArithmetics.cpp ...)
    

    with:

    add_library(Cryptograph STATIC modulArithmetics.cpp ...) # no main.cpp
    

    Using a STATIC library is one approach. You could also use a SHARED or OBJECT library. See the first reference link below for more on CMake library targets in general.

    And if you need to execute Cryptograph/main.cpp, you can turn it into its own executable that links to Cryptograph, like so in Cryptograph/CMakeLists.txt:

    add_executable(CryptographApp main.cpp)
    target_link_libraries(CryptographApp Cryptograph)
    

    Resources