# Understanding of FetchContent

**URL:** <https://discourse.cmake.org/t/understanding-of-fetchcontent/9963>\
**Category:** Code\
**Created:** [January 29, 2024, 10:38pm UTC](https://discourse.cmake.org/t/understanding-of-fetchcontent/9963 "2024-01-29T22:38:17Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Codeboybebop](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeboybebop/32/3910_2.png) [@Codeboybebop](https://discourse.cmake.org/u/Codeboybebop)\
**Post date:** [January 29, 2024, 10:38pm UTC](https://discourse.cmake.org/t/understanding-of-fetchcontent/9963/1 "2024-01-29T22:38:17Z")

</div>

I have created a [repository](https://github.com/Codeboybebop/FetchLibTest) 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

```auto
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?

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [January 30, 2024, 2:26pm UTC](https://discourse.cmake.org/t/understanding-of-fetchcontent/9963/2 "2024-01-30T14:26:08Z")

</div>

Lib is not a `namespace` and not a `target`.

Simply use

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

```

or

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

```

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [January 30, 2024, 2:29pm UTC](https://discourse.cmake.org/t/understanding-of-fetchcontent/9963/3 "2024-01-30T14:29:26Z")

</div>

by the way, setting

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

```

is not a good style!

```bash
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$ 

```

---

<div class="post-metadata">

**Author:** ![Codeboybebop](https://discourse.cmake.org/user_avatar/discourse.cmake.org/codeboybebop/32/3910_2.png) [@Codeboybebop](https://discourse.cmake.org/u/Codeboybebop)\
**Post date:** [January 30, 2024, 7:48pm UTC](https://discourse.cmake.org/t/understanding-of-fetchcontent/9963/4 "2024-01-30T19:48:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![ClausKlein](https://discourse.cmake.org/user_avatar/discourse.cmake.org/clausklein/32/352_2.png) [@ClausKlein](https://discourse.cmake.org/u/ClausKlein)\
**Post date:** [January 30, 2024, 8:49pm UTC](https://discourse.cmake.org/t/understanding-of-fetchcontent/9963/5 "2024-01-30T20:49:59Z")

</div>

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:

```auto
-- 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$ 

```
