I’m using CMake to build a project following this template.
The linking stage fails with duplicate symbols found my header files.
...
[100%] Linking CXX executable ../bin/user
duplicate symbol 'structures::BarStruct::dump()' in:
...build/src/CMakeFiles/user.dir/user.cpp.o
libLearn.a[2](FooUser.cpp.o)
duplicate symbol 'structures::FooStruct::dump()' in:
.../build/src/CMakeFiles/user.dir/user.cpp.o
libLearn.a[2](FooUser.cpp.o)
duplicate symbol 'structures::FinalStruct::dump()' in:
.../CMakeFiles/user.dir/user.cpp.o
libLearn.a[2](FooUser.cpp.o)
ld: 3 duplicate symbols
My project layout is as below.
├── CMakeLists.txt
├── README.md
├── cmake
│ └── Config.cmake.in
├── include
│ ├── FooHeader.hpp
│ └── FooUser.hpp
├── src
│ ├── CMakeLists.txt
│ ├── FooUser.cpp
│ └── user.cpp
└── tests
src/CMakeLists.txt
if(BUILD_TESTING)
# Required for self registrering unit tests for a library target.
set(PROJECT_TYPE OBJECT)
else()
# Without unit tests, build the target as a normal static library.*
set(PROJECT_TYPE STATIC)
endif()
add_library(${PROJECT_NAME} ${PROJECT_TYPE})
add_library(${PROJECT_NAME}::${PROJECT_NAME} ALIAS ${PROJECT_NAME})
target_sources(${PROJECT_NAME}
PRIVATE
FooUser.cpp
)
target_include_directories(${PROJECT_NAME}
PUBLIC
$<INSTALL_INTERFACE:include>/
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/include>
FooUser.cpp
)
target_include_directories(${PROJECT_NAME}
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}
)
add_executable(user user.cpp)
target_link_libraries(user PRIVATE ${PROJECT_NAME})
set_target_properties(user PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin
)
include/FooHeader.hpp
#ifndef FOOHEADER_H
#define FOOHEADER_H
#include <iostream>
namespace structures {
struct FooStruct {
std::vector<double> cost;
std::vector<char> type;
std::vector<int> count;
void dump();
};
struct BarStruct {
std::vector<double> cost;
std::vector<char> count;
void dump();
};
struct FinalStruct {
std::vector<int64_t> numcust;
std::vector<double> totalacctbal;
void dump();
};
} // namespace
void structures::FooStruct::dump() {
std::cout << "nFOoO";
}
void structures::BarStruct::dump() {
std::cout << "nBar";
}
void structures::FinalStruct::dump() {
std::cout << "nFinal";
}
#endif //
include/FooUser.hpp:
#ifndef FOOUSER_H
#define FOOUSER_H
#include "FooHeader.hpp"
namespace work {
void headerFunc1(const structures::FooStruct& in,
const structures::BarStruct& out);
void headerFunc2(const structures::BarStruct& in,
structures::FinalStruct& out);
} // namespace
//
#endif
src/FooUser.cpp:
#include "FooHeader.hpp"
#include "FooUser.hpp"
void work::headerFunc1(const structures::FooStruct& in,
const structures::BarStruct& ret) {
std::cout << "Working on Foo -> Bar";
}
void work::headerFunc2(const structures::BarStruct& in, structures::FinalStruct& ret) {
std::cout << "Working on Bar -> Final";
}
src/user.cpp
#include "FooUser.hpp"
#include <cassert>
bool verifyOutput(structures::FinalStruct &test) {
test.dump();
return test.numcust.size() > 0;
}
int main() {
structures::FooStruct foo;
structures::BarStruct bar;
structures::FinalStruct finalstruct;
work::headerFunc1(foo, bar);
work::headerFunc2(bar, finalstruct);
return 0;
}
CMakeLists.txt (toplevel)
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(Learn CXX)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
# GoogleTest requires at least C++14
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
include(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/03597a01ee50ed33e9dfd640b249b4be3799d395.zip
)
# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
add_subdirectory(src)
I understand the error but I’m not sure what in my CMake is causing the files to be included more than once.
I was expecting the ifndef macros to take care of this. I can get this working by squishing files together but I want to learn how to keep the two header files separate.
Platform: MacOS 15.2 with Apple Clang.
cmake-wanderer is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1
Now is the perfect time to learn about the concept of translation units.
A translation unit is the single unit that the compiler itself works with, and is basically a single source file with all included header files. The compiler create an object file from this, which the linker will use to create the executable program.
A function (or variable) can only be defined in one single translation unit (the one definition rule). When you define the function in the header file, it will be defined in all translation units where the header file is included, leading to your error.
There are two solutions to this:
- Define the function is one single source file;
- Or mark the functions as
inline