QtCreator multiple definition build bug
Asked Answered
R

4

11

this is my .pro file:

QT       += core gui widgets

TARGET = link_mult_def

TEMPLATE = app

SOURCES +=  main.cpp \
            path2/file.cpp \
            path1/file.cpp

HEADERS +=

For some reason, QtCreator does not respect the source folder structure when building the .o files from the .cpp files. Both files will be compiled to "shadow_build_directory/file.o". I would expect the build process to create path1 and path2 directories in the shadow build directory and compile "path1/file.cpp" to "shadow_build_directory/path1/file.o" and "path2/file.cpp" to "shadow_build_directory/path2/file.o".

Since the compiled symbols from both sources add up in the file.o it is not such a big problem yet. It becomes a big problem when QtCreator tries to link:

g++ -Wl,-O1 -o link_mult_def main.o file.o file.o -L/usr/lib/x86_64-linux-gnu -lQtCore -lpthread

QtCreator links file.o two times which makes the linker fail with mutiple definition error.

How can I make sure that QtCreator compiles to object files that reflect the source directory structure?

Thanks

EDIT:

path1/file.cpp

#include <iostream>
void function1()
{
    std::cout << "function1" << std::endl;
}

path2/file.cpp

#include <iostream>
void function2()
{
    std::cout << "function2" << std::endl;
}

Build process by QtCreator:

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../link_mult_def -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I../link_mult_def -I. -o main.o ../link_mult_def/main.cpp

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../link_mult_def -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I../link_mult_def -I. -o file.o ../link_mult_def/path1/file.cpp

g++ -c -pipe -O2 -Wall -W -D_REENTRANT -DQT_WEBKIT -DQT_NO_DEBUG -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../link_mult_def -I/usr/include/qt4/QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I../link_mult_def -I. -o file.o ../link_mult_def/path2/file.cpp

g++ -Wl,-O1 -o link_mult_def main.o file.o file.o    -L/usr/lib/x86_64-linux-gnu -lQtGui -lQtCore -lpthread 

file.o: In function `function2()':
file.cpp:(.text+0x0): multiple definition of `function2()'
make: Leaving directory `/home/schmid/code/misc/trash/link_mult_def-build-desktop-Qt_4_8_1_in_PATH__System__Release'
file.o:file.cpp:(.text+0x0): first defined here
collect2: error: ld returned 1 exit status
make: *** [link_mult_def] Error 1
Rudd answered 11/9, 2012 at 8:55 Comment(3)
Actually it's the way QtCreator that uses qmake. I dont tell anything to qmake. I just add source files to my project. I need all the files to build my executable. But when i build the project it fails.Rudd
i am pretty sure that the source code is fine. It is a problem with QtCreator. As you can see both source files are compiled to the same object file which appears two times in the linker call. Which leads to mutliple definition error. I dont know how to tell QtCreator to compile to seperate object files.Rudd
Sorry about that, I hadn't seen you used the same filename in the subdirectories... sorry for the noise.Callisto
O
1

If you're comfortable having your object files alongside your source files, then you can use either

CONFIG += object_parallel_to_source

or

CONFIG += object_with_source

depending on which version of QMake you're using.

Answer cribbed from SO answer here.

Opposite answered 15/4, 2016 at 16:44 Comment(0)
L
0

I had the exact same problem with Visual Studio in the past. What it does is it compiles and places all object files in one directory, just like in your case. We worked around that by not having duplicate file names in a project.

If what you say is true - that the QtCreator places all object files in one directory, then all you can do is name your files with unique names per project.

List answered 11/9, 2012 at 9:30 Comment(1)
I added the build steps from QtCreator. You can see that it compiles to the same object file. Unfortunately I am not allowed to change the source structure because it is not my code...Rudd
B
0

Perhaps you could divide the entire project into two ones (without touching the existing source files configuration, just managing the .pro files) and set up the dependencies between them. Then for each project you could set the own output build files directory (see, for example, here).

Bishop answered 11/9, 2012 at 9:59 Comment(0)
H
-1

The solution would be to just rename your files. The directories and the dir structure of your project have nothing to do with the compiler. The compiler doesn't even care where the files are, it just needs to get the files, no matter if the files are in /src folder, or on the Moon.

Now obviously, after the .o files are made you get the error, simply because you have two files of the same name.

Even if you did not have this problem, making multiply files with the same name is bad, especially if the names are meaningless, as file.cpp.

Horme answered 11/9, 2012 at 9:41 Comment(2)
I must not rename files because it is not my code and i must not change the code structure. People will kill me if I start to rename files. I think the problem is, that we have source files seperated in directories but the build process does not seperate the object files in the same way.Rudd
"Even if you did not have this problem, making multiply files with the same name is bad, especially if the names are meaningless, as file.cpp." I created a minimal example, thats why the files are named "file.cpp". I dont think it is bad to have files with the same name. That is why we have directories in file systems. Imagine a huge project with source code distributed over hundres of subdirectories. You have different teams working on different subdirectories and you can not tell them not to used same file names as in other subdirectories...Rudd

© 2022 - 2024 — McMap. All rights reserved.