开发者

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

开发者 https://www.devze.com 2023-04-13 03:47 出处:网络
My Qt project have two source files with the same name but in different folder. The pro file is: SOURCES = A/Test.cpp

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 sam开发者_开发知识库e 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?


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 newer

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


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.


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.


One solution is to rename the files:

A/a_Test.cpp 
B/b_Test.cpp

It's a bit ugly, but its simple and the class names can stay the same.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号