Building the following cmake
project from the trivial Qt6
project below gives me the following linker error when I #include "utils.hpp"
from mainwindow.h
:
duplicate symbol 'get_test_map()' in:
CMakeFiles/qt_minimal.dir/qt_minimal_autogen/mocs_compilation.cpp.o
CMakeFiles/qt_minimal.dir/src/gui/main.cpp.o
duplicate symbol 'get_test_map()' in:
CMakeFiles/qt_minimal.dir/qt_minimal_autogen/mocs_compilation.cpp.o
CMakeFiles/qt_minimal.dir/src/gui/mainwindow.cpp.o
ld: 2 duplicate symbols for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
According to an answer to a similar question, having the #include "utils.hpp"
in mainwindow.cpp
instead solves the linker error. However, I would like the class MainWindow
to call functions from external headers which requires having the include in MainWindow.h
itself.
How do I fix this issue?
CMakeLists.txt
cmake_minimum_required(VERSION 3.24)
project(qt_minimal)
set(CMAKE_CXX_STANDARD 20)
find_package(Qt6 COMPONENTS Widgets REQUIRED)
set(CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
include(QtCommon)
set(GUI_SOURCE_FILES
main.cpp
mainwindow.cpp
)
add_executable(${PROJECT_NAME} ${OS_BUNDLE} # Expands to WIN32 or MACOS_BUNDLE depending on OS
${GUI_SOURCE_FILES} ${META_FILES_TO_INCLUDE} ${RESOURCE_FILES}
)
target_precompile_headers(${PROJECT_NAME} INTERFACE QtWidgets.h)
target_link_libraries(${PROJECT_NAME} Qt6::Widgets)
main.cpp
#include <QApplication>
#include "mainwindow.h"
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
MainWindow mainWindow;
mainWindow.show();
return app.exec();
}
utils.hpp
#ifndef QT_PROJECT_GUI_UTILS_HPP
#define QT_PROJECT_GUI_UTILS_HPP
#include <unordered_map>
using Map = std::unordered_map<std::string, std::string>;
Map get_test_map() {
Map m;
m["accountID"] = "LKH7876";
m["BankBalance"] = "1,00,000";
m["LoanAmt"] = "10,00,000";
return m;
}
#endif
mainwindow.h
#ifndef mainwindow_h
#define mainwindow_h
#include <QMainWindow>
#include "utils.hpp" // <-- Offending include
namespace Ui
{
class MainWindow;
}
class MainWindow : public QMainWindow {
Q_OBJECT
public:
MainWindow(QWidget *parent = 0);
virtual ~MainWindow();
private:
QScopedPointer<Ui::MainWindow> ui;
};
#endif
mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent),
ui(new Ui::MainWindow) {
ui->setupUi(this);
// do something with get_test_map()
}
MainWindow::~MainWindow() = default;
QTCommon.cmake
macro(fix_project_version)
if (NOT PROJECT_VERSION_PATCH)
set(PROJECT_VERSION_PATCH 0)
endif()
if (NOT PROJECT_VERSION_TWEAK)
set(PROJECT_VERSION_TWEAK 0)
endif()
endmacro()
macro(add_project_meta FILES_TO_INCLUDE)
if (NOT RESOURCE_FOLDER)
set(RESOURCE_FOLDER src/gui/res)
endif()
if (NOT ICON_NAME)
set(ICON_NAME AppIcon)
endif()
if (APPLE)
set(ICON_FILE ${RESOURCE_FOLDER}/${ICON_NAME}.icns)
elseif (WIN32)
set(ICON_FILE ${RESOURCE_FOLDER}/${ICON_NAME}.ico)
endif()
if (WIN32)
configure_file("${PROJECT_SOURCE_DIR}/cmake/windows_metafile.rc.in"
"windows_metafile.rc"
)
set(RES_FILES "windows_metafile.rc")
set(CMAKE_RC_COMPILER_INIT windres)
ENABLE_LANGUAGE(RC)
SET(CMAKE_RC_COMPILE_OBJECT "<CMAKE_RC_COMPILER> <FLAGS> -O coff <DEFINES> -i <SOURCE> -o <OBJECT>")
endif()
if (APPLE)
set_source_files_properties(${ICON_FILE} PROPERTIES MACOSX_PACKAGE_LOCATION Resources)
# Identify MacOS bundle
set(MACOSX_BUNDLE_BUNDLE_NAME ${PROJECT_NAME})
set(MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION})
set(MACOSX_BUNDLE_LONG_VERSION_STRING ${PROJECT_VERSION})
set(MACOSX_BUNDLE_SHORT_VERSION_STRING "${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR}")
set(MACOSX_BUNDLE_COPYRIGHT ${COPYRIGHT})
set(MACOSX_BUNDLE_GUI_IDENTIFIER ${IDENTIFIER})
set(MACOSX_BUNDLE_ICON_FILE ${ICON_NAME})
endif()
if (APPLE)
set(${FILES_TO_INCLUDE} ${ICON_FILE})
elseif (WIN32)
set(${FILES_TO_INCLUDE} ${RES_FILES})
endif()
endmacro()
macro(init_os_bundle)
if (APPLE)
set(OS_BUNDLE MACOSX_BUNDLE)
elseif (WIN32)
set(OS_BUNDLE WIN32)
endif()
endmacro()
macro(fix_win_compiler)
if (MSVC)
set_target_properties(${PROJECT_NAME} PROPERTIES
WIN32_EXECUTABLE YES
LINK_FLAGS "/ENTRY:mainCRTStartup"
)
endif()
endmacro()
macro(init_qt)
# Let's do the CMake job for us
set(CMAKE_AUTOMOC ON) # For meta object compiler
set(CMAKE_AUTORCC ON) # Resource files
set(CMAKE_AUTOUIC ON) # UI files
endmacro()
init_os_bundle()
init_qt()
fix_win_compiler()