7

I have a project with some folders which happen to contain source files with the same names.

My source tree looks like this:

project.pro

foo/
    conflict.h
    conflict.cpp

bar/
    conflict.h
    conflict.cpp

some.h
other.h
files.h
main.cpp

Per default, qmake generates a Makefile which will produce a build tree like this:

conflict.o
main.o
target

Where conflict.o is the object file resulting for both foo/conflict.cpp and foo/conflict.h.

I can't to change their names because they are generated using an external tool and forcing different file names would imply to change their contents, so this is not an option.

I also don't want to use qmake SUBDIRS template because this would imply that (1) every subdir is built separately as a library and thus very much complicate the overall build process (in my eyes at least) and (2) in the top level directory I can't have any source files. Or am I wrong?

Can't I just tell qmake to write the object files into separate directories within the build directory? So my build tree will look like this:

foo/
    conflict.o

bar/
    conflict.o

main.o
target

Or are there any other solutions neither requiring to rename the source files nor introducing something complicated like static libraries? I just can't believe that Qt didn't solve this (in my eyes simple) problem for years. (I already hat this problem 4 years ago but could rename the files in that project, while here I can't.)

If it's important: I use Qt 4.8 on both Ubuntu with G++ and Windows with mingw32.

leemes
  • 44,967
  • 21
  • 135
  • 183
  • @NikosC. Can I have a pro file for the top level files which will link against the other libraries + one top level SUBDIRS pro file which will have the subdir `.`? This would require that I also overwrite the .pro file for this subdir to be something different than the top level .pro file for the whole app. I hope you understand what I mean. I just want to avoid that I have to move all top level files into a physical subdirectory. – leemes Nov 01 '12 at 21:18
  • You should probably use only one additional project file for `foo`, and keep you existing one for the top level dir and `bar`. Another option is to symlink the conflicting filename: `ln -s bar/conflict.cpp bar/conflict_bar.cpp` and then use the symlink in the project file. – Nikos C. Nov 01 '12 at 21:25
  • @NikosC. Thanks but symlinking also is no option because all generated files are also put into a generated .pri file for inclusion in some .pro file. I'm currently completely rewriting the build process to use SUBDIRS. I'm very disappointed in qmake that I really need to do this but I can't see a better way... All other solutions sound just wrong. If i only created the additional .pro for `foo`, how can I tell the main .pro to use this? I can't use subdirs+app template together in the main file, can I? – leemes Nov 01 '12 at 21:36
  • Seems to be a duplicate of this one: http://stackoverflow.com/questions/7765147/how-to-use-qmake-with-two-source-files-which-have-the-same-name – johsin18 Apr 04 '16 at 12:04

1 Answers1

2

Are you tied to qmake? If not, an alternative could be to use cmake. I just verified your usecase with a simple CMakeLists.txt like

cmake_minimum_required (VERSION 2.6)
project (conflict)
add_executable(conflict foo/conflict.cpp bar/conflict.cpp main.cpp)

which even included a source file in the top level directory (main.cpp). This properly builds the executable - the object files are created in sub directories like

./CMakeFiles/conflict.dir/main.cpp.o
./CMakeFiles/conflict.dir/bar/conflict.cpp.o
./CMakeFiles/conflict.dir/foo/conflict.cpp.o

cmake also includes support for Qt4, to automatically pull in the required include paths and libraries. It might require some effort to migrate from qmake to cmake, but given the requirements you have I would give it a try.

Andreas Fester
  • 36,091
  • 7
  • 95
  • 123
  • The problem is that for the generated code files also a .pri file is generated which helps to bind all generated files to an actual project. But since the code generator is written by me, I could rewrite it to use cmake, however, this would be even more complicated than using qmake's subdirs, I think. But this would be a good solution for other users, so your answer fits well :) – leemes Nov 01 '12 at 21:40
  • Too bad ;) Then, probably the easiest way is indeed to use subdirs, probably building static libraries, and finally link them all together. – Andreas Fester Nov 01 '12 at 21:42
  • Yeah. But I want to have a root project which is not a subdirs project but still will depend on building the subdirs... I don't know how I can do this. – leemes Nov 01 '12 at 21:48
  • 2
    I ended up using qmake `SUBDIRS` with libraries. However, I accept your answer as it is a nice alternative and very useful for others... – leemes Dec 08 '12 at 20:55