Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use qmake with two source files which have the same name?

My Qt project have two source files with the same name but in different folder. The pro file is:

SOURCES = A/Test.cpp 
SOURCES += B/Test.cpp

It can generate Visual Studio solution file via Qt Visual Studio addon, but it won't work because the generated object file have the same name: Test.obj. That will cause LNK2001 unresolved external symbol because one of Test.obj is overwritten.

How to write proper pro file to deal with that?

like image 884
Bear Avatar asked Oct 14 '11 08:10

Bear


3 Answers

Before Qt 5

You can try adding that line to your .pro file:

CONFIG += object_with_source

But as the option name implies, the .obj files will not be created in the out-of-source / "shadow build" directory.

Qt 5 and older

That option has been replaced by object_parallel_to_source in Qt 5, which should work with the shadow building.

like image 150
alexisdm Avatar answered Nov 09 '22 06:11

alexisdm


You should consider splitting your solution in multiple projects, but it depends if each one of those folders could represent a project by its own. If you choose this solution, you will have to write one .pro file per project. The usual way to go is to write a 'generic' *.pri file which is included from every *.pro file:

folder1.pro

TEMPLATE=lib
TARGET=folder1
include( ../common.pri )

folder2.pro

TEMPLATE=lib
TARGET=folder2
include( ../common.pri )

common.pri (in parent directory)

SOURCES += *.cpp
HEADERS += *.h
# etc.

Obviously the contents of each pro file depends on your solution.

If you don't want to split the source files in multiple projects, the easier solution would be to rename one the conflicting files, I guess.

like image 4
azf Avatar answered Nov 09 '22 04:11

azf


I recently came across this issue, too. Splitting the project into subprojects made everything much more complicated, and -at least on my first attempt- flat-out didn't work. Then I tried CONFIG += object_with_source and CONFIG += object_parallel_to_source, but both did't seem to work with my Qt version.

So this is how I solved it (for Visual Studio 2010; I don't know if works the same with other versions):

If the project were an ordinary Visual Studio project, not one generated by QMake, you could solve it as described here: Visual Studio 2010 & 2008 can't handle source files with identical names in different folders? (changing the output dir of object files to a relative dir by appending %(RelativeDir) in the project settings "C/C++" > "Output Files" > "Object File Name").

Obviously, you don't want to do this by hand everytime you create a new Visual Studio project with QMake, so why not automatize it? After all Visual Studio project files are but ordinary XML files. Looking at the diff before and after setting the options reveals it's saved in a single unique tag called ObjectFileName.

So I wrote this Python script:

import sys

filename = sys.argv[1]
f = open(filename, "r", -1, "utf-8-sig")
lines = f.readlines()
f.close()
f = open(filename, "w", -1, "utf-8-sig")
for line in lines:
    line = line.replace("</ObjectFileName>", "%(RelativeDir)\</ObjectFileName>")
    f.write(line)
f.close()

..and use it like this in my bat-file that I always call to create the Visual Studio project:

qmake -tp vc myproject.pro
@cd ../scripts
unflatten_vcproj_obj_output.py "../src/myproject.vcxproj"
@pause

Not a beautiful solution, but it works.

like image 1
Sebastian Negraszus Avatar answered Nov 09 '22 06:11

Sebastian Negraszus