Understanding of FetchContent

I have created a repository that contains two libraries foo and bar. They define same functions std::string call() but provides different result. I want to use one of them in different project. Hence I wrote the following CMakeList

cmake_minimum_required(VERSION 3.28)
project(FetchLibTestReceive CXX)

set(CMAKE_CXX_STANDARD 20)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++ ")

Include(FetchContent)

FetchContent_Declare(
        Lib
        GIT_REPOSITORY https://github.com/Codeboybebop/FetchLibTest
)

FetchContent_MakeAvailable(Lib)

add_executable(testing_fetch test.cpp)
target_link_libraries(testing_fetch PRIVATE Lib)

How to specify correctly what library will be linked? I tried to write Lib::foo but it can’t find that target.

Also, If I have another target in repository that contains those libraries, and if I link one of the library to this target, then this library will be linked in new project. Why does that work this way?

Lib is not a namespace and not a target.

Simply use

add_executable(testing_foo test.cpp)
target_link_libraries(testing_foo PRIVATE foo)

or

add_executable(testing_bar test.cpp)
target_link_libraries(testing_bar PRIVATE bar)

by the way, setting

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lstdc++ ")

is not a good style!

bash-5.2$ ninja -C build
ninja: Entering directory `build'
[1/16] Scanning /Users/clausklein/Workspace/cpp/cxx20/test/build/_deps/fetchlibtest-src/foo.cppm for CXX dependencies
warning: -Z-reserved-lib-stdc++: 'linker' input unused
[2/16] Scanning /Users/clausklein/Workspace/cpp/cxx20/test/build/_deps/fetchlibtest-src/bar.cppm for CXX dependencies
warning: -Z-reserved-lib-stdc++: 'linker' input unused
[9/16] Building CXX object _deps/fetchlibtest-build/CMakeFiles/bar.dir/bar.cppm.o
clang++: warning: -Z-reserved-lib-stdc++: 'linker' input unused [-Wunused-command-line-argument]
[10/16] Building CXX object _deps/fetchlibtest-build/CMakeFiles/foo.dir/foo.cppm.o
clang++: warning: -Z-reserved-lib-stdc++: 'linker' input unused [-Wunused-command-line-argument]
[16/16] Linking CXX executable testing_bar
bash-5.2$ 

Should I use add_compile_options? I don’t understand what is the problem from your logs.

If you really need it, use set(CMAKE_EXE_LINKER_FLAGS -lstdc++)

I deleted it completely and it still works without any warning like this:

-- Build files have been written to: /Users/clausklein/Workspace/cpp/cxx20/test/build
[1/2] : && /usr/local/opt/llvm/bin/clang++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -mmacosx-version-min=13.6 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lstdc++ CMakeFiles/testing_foo.dir/test.cpp.o -o testing_foo  _deps/fetchlibtest-build/libfoo.a && :
ld: warning: ignoring duplicate libraries: '-lc++'
[2/2] : && /usr/local/opt/llvm/bin/clang++ -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX14.2.sdk -mmacosx-version-min=13.6 -Wl,-search_paths_first -Wl,-headerpad_max_install_names -lstdc++ CMakeFiles/testing_bar.dir/test.cpp.o -o testing_bar  _deps/fetchlibtest-build/libbar.a && :
ld: warning: ignoring duplicate libraries: '-lc++'
bash-5.2$