I keep getting a linker error

Slowly losing my sanity over here…

My root is:

.
├── 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:

# 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:

/**
 * Logging helpers.
 */

#pragma once


namespace LoggingHelper {

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

}  // namespace LoggingHelper

Functions are thus defined in logging_helper.cpp:

/**
 * 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:

# 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:

#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:

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

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 and tried to apply it to my project.

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

1 Like

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.

1 Like