Duplicate Symbols while Linking CMake Project [duplicate]

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.

New contributor

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:

  1. Define the function is one single source file;
  2. Or mark the functions as inline

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật