What's the cause of this link error when I try to link my executable to my library by name?

kvrier

I'm trying to build and run project that links files based on the picked C++ standard. The project file structure looks like this:

.
├── CMakeLists.txt
├── cpp-version-test
│   ├── CMakeLists.txt
│   ├── test-cpp17.cpp
│   ├── test-cpp17.hpp
│   ├── test-cpp20.cpp
│   └── test-cpp20.hpp
└── main.cpp

In the top CMakeLists.txt I set C++ standard like this:

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED True)

Inside cpp-version-test CMakeLists.txt file I run conditional:

if(CMAKE_CXX_STANDARD GREATER_EQUAL 20)
    message("C++20 choosen, building test-cpp20")
    add_library(cpp_version_test OBJECT test-cpp20.cpp)
else()
    message("C++20 not choosen, building test-cpp17")
    add_library(cpp_version_test OBJECT test-cpp17.cpp)
endif()

target_include_directories(cpp_version_test INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})

And back inside the top CMakeLists.txt I glue it with the following snippet:

add_subdirectory(cpp-version-test)
list(APPEND EXTRA_LIBS cpp-version-test)

add_executable(Template main.cpp)
target_link_libraries(Template PUBLIC ${EXTRA_LIBS} compiler_flags)

target_include_directories(Template PUBLIC
    "${PROJECT_BINARY_DIR}"
)

cmake --build runs fine, Clang clogs up on linking:

[ 33%] Building CXX object CMakeFiles/Template.dir/main.cpp.o
[ 66%] Linking CXX executable Template
/usr/bin/ld: cannot find -lcpp-version-test: No such file or directory
clang-15: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [CMakeFiles/Template.dir/build.make:97: Template] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/Template.dir/all] Error 2
make: *** [Makefile:91: all] Error 2

Any help would be greatly appreciated. It's my back and forth with learning CMake.

user

You do this in your subdirectory:

add_library(cpp_version_test OBJECT ...)

Then in your top CMakeLists.txt:

add_subdirectory(cpp-version-test)
list(APPEND EXTRA_LIBS cpp-version-test)
add_executable(Template main.cpp)
target_link_libraries(Template PUBLIC ${EXTRA_LIBS} compiler_flags)

You mixed up the name of your directory and the name of your target. You target is named with underscores and your directory with dashes. target_link_libraries works with target names and not directory names. Your "list(APPEND EXTRA_LIBS cpp-version-test)" should instead be "list(APPEND EXTRA_LIBS cpp_version_test)".

Some additional feedback: Instead of doing things the way you're doing, I would let the user set a cache variable named something like "CPP_VERSION_TEST_CXX_LANGUAGE_STANDARD" to choose the number of the C++ language version to use and then target_compile_features(cpp_version_test <VISIBILITY> "cxx_std_${CPP_VERSION_TEST_CXX_LANGUAGE_STANDARD}"). See docs:

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

"Accounts failed to link" error happens when I try to authorize Google Assistant at my Web service

Android http URL scheme is not working when I try to launch my application from a link on my website

Error: Invariant failed: You should not use <Link> outside a <Router> when I try to test my app using localhost

I am not able to figure out a bug that is when I try to click on a link on my website it appears the 404

How do I statically link zstd library to my dynamic library?

My CMS is stripping out the ? at the end of a URL when I try to save the link... is there a way to make it stick?

How do I link cabal library and executable?

Have an a tag with a link to another website inside my data.jsx array but it says link not found when I try to visit it on my local dev

How can I link my C program against the Arb library?

I get a link error when i add a constructor to my derived class

I get an error when I try to change my upload image function name Django

Link a domain name to my router's public IP

What is this error I get when I try to hash my password in symfony 4

I want to add image when I share my website link

What to do when hard link is lost because of my text editor

Unwanted underline shows up on my header's link when I move the mouse over it

flutter build won't find a dependecy but when i copy the link to my browser it's at the same location

Why can't I link my CSS when it's outside the root directory?

My function's click only triggers when I click the link/button twice

What's wrong with my Regex string for scraping link elements

What's the link to my DevOps repo for integration with Git?

What is the error in my code that once I view all I get the message import androidx.appcompat.app.AppCompatActivity? link for the error in the picture

How do I display my error message as a link in Laravel

Cmake: link openscenegraph to my shared library

How to link to my own library in gcc?

when I click on link on my website it is not opening? why is it so?

Why does the position of my text change when I make it a link?

Facebook "hijacks" my Android app when I post a link to it

No pictures are showing up in Facebook when I post the link to my site