1

EDIT: Thanks to botje below for the pointer. Switching it to inline worked, as well as shifting the definition out to a cpp file

I'm using Automoc with my CMake project and I keep getting this error below.

I don't see why this would result in duplicate symbols? It seems like automoc is to blame here but it's hard to test that since I need to run moc on these files anyway.

Error

duplicate symbol __Z3foov in:
    CMakeFiles/Foo.dir/main.cpp.o
    CMakeFiles/Foo.dir/MainWindow.cpp.o
duplicate symbol __Z3foov in:
    CMakeFiles/Foo.dir/main.cpp.o
    CMakeFiles/Foo.dir/Foo_autogen/mocs_compilation.cpp.o
ld: 2 duplicate symbols for architecture x86_64

I can't see what would cause this error at all. Here's a very minimal project that replicates the issue. A CMake file below and the files in my project.

I'm compiling this on a Mac and from inside CLion, though of course the error happens outside of CLion too since it's not doing anything special.

CMakeLists.txt

cmake_minimum_required(VERSION 3.13)
project(Foo)

set(CMAKE_CXX_STANDARD 17)

set(SOURCE_FILES
        main.cpp MainWindow.cpp MainWindow.h constants.h)

find_package(Qt5
        ${QT5_USE_VERSION}
        COMPONENTS
        Widgets
        Core

        PATHS
        /usr/local/opt/qt5

        REQUIRED)

set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
add_executable(${PROJECT_NAME} ${SOURCE_FILES})

qt5_use_modules(${PROJECT_NAME} Widgets Core)

main.cpp

#include <QApplication>
#include <QPushButton>


#include "MainWindow.h"


int main(int argc, char **argv) {
    QApplication app(argc, argv);

    MainWindow win;
    win.show();

    return app.exec();
}

MainWindow.h

#ifndef FOO_MAINWINDOW_H
#define FOO_MAINWINDOW_H

#include <QMainWindow>
#include "constants.h"


class MainWindow: public QMainWindow {

    Q_OBJECT

};


#endif //FOO_MAINWINDOW_H

mainWindow.cpp

// Empty File

constants.h

#ifndef FOO_CONSTANTS_H
#define FOO_CONSTANTS_H

int foo() {
    return 0;
}

#endif //FOO_CONSTANTS_H

Anyway any help would be appreciated, I'm hitting a wall with this and it looks like it should be something simple that I'm simply missing.

Dhruv Govil
  • 349
  • 1
  • 3
  • 13
  • 2
    Your `constants.h` defines a `foo` function. Either mark it as `inline` so the compiler can squash multiple definitions or move its definition to a cpp file of your choosing. – Botje Feb 05 '19 at 06:57

0 Answers0