# link option wrong with -latomic

**URL:** https://discourse.cmake.org/t/link-option-wrong-with-latomic/7173
**Category:** Code
**Tags:** os:linux, comp:gcc, gen:makefiles
**Created:** [January 2, 2023, 9:46am UTC](https://discourse.cmake.org/t/link-option-wrong-with-latomic/7173 "2023-01-02T09:46:04Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![dglz.hyj](https://discourse.cmake.org/user_avatar/discourse.cmake.org/dglz.hyj/32/2836_2.png) [@dglz.hyj](https://discourse.cmake.org/u/dglz.hyj)
#### Post date: [January 2, 2023, 9:46am UTC](https://discourse.cmake.org/t/link-option-wrong-with-latomic/7173/1 "2023-01-02T09:46:04Z")

</div>

```cpp
#include "thread"
#include "iostream"
#include "atomic"

using namespace std;

struct A { int a[100]; };
struct B { int x, y; };
int main()
{
    std::atomic<A> Ap ;
    std::atomic<B> Bp ;

    std::cout << std::boolalpha
              << "std::atomic<A> is lock free? "
              << Ap.is_lock_free() << '\n'
              << "std::atomic<B> is lock free? "
              << Bp.is_lock_free() << '\n';
    return 0;
}

```

when I compile it with gcc11 Ubuntu22.04 with makefile ,  
it show me undefined reference to `\_\_atomic\_is\_lock\_free’.

So I use `g++ main.cpp -latomic` and it compile without error . great!!!

But what shoul I do if I want to add -latomic in cmake ? I try a lot of time…

Here is my cmake code

```cmake

cmake_minimum_required(VERSION 3.20)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)

project(mlt)
add_executable(mlt main.cpp)

target_compile_options(mlt PUBLIC -latomic)
target_link_options(mlt PUBLIC -latomic)
#set(CMAKE_EXE_LINKER_FLAGS -latomic )

```

---

<div class="post-metadata">

### Author: ![marc.chevrier](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/m/ecb155/32.png) [@marc.chevrier](https://discourse.cmake.org/u/marc.chevrier)
#### Post date: [January 2, 2023, 9:56am UTC](https://discourse.cmake.org/t/link-option-wrong-with-latomic/7173/2 "2023-01-02T09:56:27Z")

</div>

Use [target\_link\_libraries()](https://cmake.org/cmake/help/latest/command/target_link_libraries.html) command:

```cmake
cmake_minimum_required(VERSION 3.20)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_CXX_STANDARD 17)

project(mlt)
add_executable(mlt main.cpp)

target_link_libraries(mlt PUBLIC atomic)

```

---

<div class="post-metadata">

### Author: ![malaterre](https://discourse.cmake.org/user_avatar/discourse.cmake.org/malaterre/32/1971_2.png) [@malaterre](https://discourse.cmake.org/u/malaterre)
#### Post date: [January 4, 2023, 11:38am UTC](https://discourse.cmake.org/t/link-option-wrong-with-latomic/7173/3 "2023-01-04T11:38:54Z")

</div>

`atomic` should definitely be linked using PRIVATE (low level implementail detail of GCC)
