# I keep getting a linker error

**URL:** <https://discourse.cmake.org/t/i-keep-getting-a-linker-error/10557>\
**Category:** Code\
**Tags:** os:linux, os:windows\
**Created:** [April 3, 2024, 10:41am UTC](https://discourse.cmake.org/t/i-keep-getting-a-linker-error/10557 "2024-04-03T10:41:49Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shepard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shepard/32/4416_2.png) [@Shepard](https://discourse.cmake.org/u/Shepard)\
**Post date:** [April 3, 2024, 10:41am UTC](https://discourse.cmake.org/t/i-keep-getting-a-linker-error/10557/1 "2024-04-03T10:41:49Z")

</div>

Slowly losing my sanity over here…

My root is:

```auto
.
├── apps
│ ├── app.cpp
│ └── CMakeLists.txt
├── CMakeLists.txt
├── include
│ └── modern
│ └── logging_helper.hpp
├── README.md
├── src
│ ├── CMakeLists.txt
│ └── logging_helper.cpp
└── tests
    ├── CMakeLists.txt
    └── testlib.cpp

```

Then I have my root CMakeLists:

```auto
# Works with 3.14 and tested through 3.29
cmake_minimum_required(VERSION 3.14...3.29)

# Project name and a few useful settings. Other commands can pick up the results
project(
  ModernCMakeExample
  VERSION 0.1
  DESCRIPTION "An example project with CMake"
  LANGUAGES CXX)

# Only do these if this is the main project, and not if it is included through add_subdirectory
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)

  # Optionally set things like CMAKE_CXX_STANDARD, CMAKE_POSITION_INDEPENDENT_CODE here

  # Let's ensure -std=c++xx instead of -std=g++xx
  set(CMAKE_CXX_EXTENSIONS OFF)

  # Let's nicely support folders in IDEs
  set_property(GLOBAL PROPERTY USE_FOLDERS ON)

  # Testing only available if this is the main app
  # Note this needs to be done in the main CMakeLists
  # since it calls enable_testing, which must be in the
  # main CMakeLists.
  include(CTest)

  # Docs only available if this is the main app
  find_package(Doxygen)
  if(Doxygen_FOUND)
    add_subdirectory(docs)
  else()
    message(STATUS "Doxygen not found, not building docs")
  endif()
endif()

# FetchContent added in CMake 3.11, downloads during the configure step
# FetchContent_MakeAvailable was added in CMake 3.14; simpler usage
include(FetchContent)

# Formatting library
FetchContent_Declare(
  fmtlib
  GIT_REPOSITORY https://github.com/fmtlib/fmt.git
  GIT_TAG 5.3.0)
FetchContent_MakeAvailable(fmtlib)
# Adds fmt::fmt

# The compiled library code is here
add_subdirectory(src)

# The executable code is here
add_subdirectory(apps)

# Testing only available if this is the main app
# Emergency override MODERN_CMAKE_BUILD_TESTING provided as well
if((CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME OR MODERN_CMAKE_BUILD_TESTING)
   AND BUILD_TESTING)
  add_subdirectory(tests)
endif()

```

My logging\_helper.hpp has only declarations and is more or less a wrapper for SPDLOG:

```auto
/**
 * Logging helpers.
 */

#pragma once

namespace LoggingHelper {

    // Adds greetings to logger.
    inline void addGreetings();

} // namespace LoggingHelper

```

Functions are thus defined in logging\_helper.cpp:

```auto
/**
 * Logging helpers.
 */

#include <modern/logging_helper.hpp>
#include <iostream>

// Adds greetings to logger.
namespace LoggingHelper {
    inline void addGreetings() {
        std::cout << "HELLO" << "\n";
    }
}

```

my `src/CMakeLists.txt`:

```auto
# Note that headers are optional, and do not affect add_library, but they will not
# show up in IDEs unless they are listed in add_library.

# Optionally glob, but only for CMake 3.12 or later:
# file(GLOB HEADER_LIST CONFIGURE_DEPENDS "${ModernCMakeExample_SOURCE_DIR}/include/modern/*.hpp")
set(HEADER_LIST "${ModernCMakeExample_SOURCE_DIR}/include/modern/logging_helper.hpp")

# Make an automatic library - will be static or dynamic based on user setting
add_library(modern_library logging_helper.cpp ${HEADER_LIST})

# We need this directory, and users of our library will need it too
target_include_directories(modern_library PUBLIC ../include)

# All users of this library will need at least C++11
target_compile_features(modern_library PUBLIC cxx_std_11)

# IDEs should put the headers in a nice place
source_group(
  TREE "${PROJECT_SOURCE_DIR}/include"
  PREFIX "Header Files"
  FILES ${HEADER_LIST})

```

and finally my app.cpp:

```auto
#include <modern/logging_helper.hpp>

#include <vector>

int main() {
    std::vector<double> input = {1.2, 2.3, 3.4, 4.5};

    LoggingHelper::addGreetings();

    return 0;
}

```

with app/CMakeLists.txt:

```auto
add_executable(app app.cpp)
target_compile_features(app PRIVATE cxx_std_17)

target_link_libraries(app PRIVATE modern_library)

```

# Problem

constantly getting a linker error and I can’t figure out why

```auto
apps/app.cpp:8: undefined reference to `LoggingHelper::getLevelFromString(std::basic_string_view<char, std::char_traits<char> >)'

```

I don’t understand this, the src/CMakeLists.txt adds my logging\_helper library to target “modern\_library”. And this is then linked in my app/CMakeLists.txt.

Could somebody help?

## Note

I took [this example](https://gitlab.com/CLIUtils/modern-cmake/-/tree/master/examples/extended-project) and tried to apply it to my project.

---

<div class="post-metadata">

**Author:** ![Shepard](https://discourse.cmake.org/user_avatar/discourse.cmake.org/shepard/32/4416_2.png) [@Shepard](https://discourse.cmake.org/u/Shepard)\
**Post date:** [April 3, 2024, 12:39pm UTC](https://discourse.cmake.org/t/i-keep-getting-a-linker-error/10557/2 "2024-04-03T12:39:20Z")

</div>

Ok I figured out it is the `inline`. This brakes the linker.

---

<div class="post-metadata">

**Author:** ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)\
**Post date:** [April 11, 2024, 4:30pm UTC](https://discourse.cmake.org/t/i-keep-getting-a-linker-error/10557/3 "2024-04-11T16:30:16Z")

</div>

Specifically, it looks like you have an `inline` declaration in `logging_helper.hpp` but the definition is hidden in `logging_helper.cpp`, right? Functions marked `inline` must have a definition available to all callers of it, not hidden away in a separate TU.
