Trouble compiling files with identical filenames
I am currently working on a project which contains some duplicate class names. To prevent them from clashing I am using namespaces. Then I can use those classes the following way
#include "core/audio.hpp" // Audio class #include "core/database/dbaudio.hpp" // db::Audio class
Now I really dislike the fact of needing the extra
db-prefix for the database class. I tried changing the class name to just
audio.hppbut that raised an error while compiling with MSVC.
Then I googled around and found the qmake config values
object_parallel_to_source(and its older brother
object_with_source). When compiling the project using
CONFIG += object_parallel_to_sourceI get the compiler error
LNK1181: File "release/src/main.obj" cannot be opened..
Is there any way to fix that and use multiple files with the same name inside my Qt project? Does it perhaps have something to do with the fact, that I am compiling into a
bindir inside my repository?
Putting aside the fact that using same file names for different files (and classes!) is very confusing - and thus makes code maintenance harder.
Do you use proper header guards in your header files? Ifdefs or pragma or both?
Yes, I am using proper include guards. What would a good way of naming those classes? I am using
Audiomostly as a data class and
db::Audioas an "interface" for the database. I used to call it
DbAudiobut then I had lots of classes prefixed
Dband decided to put them all into a namespace.
The current main problem is keeping the class names as easy as possible without name clashing :(
If it is an interface, it can be called AudioInterface. Or as some people do: IAudio.
If it holds data, it could be AudioData, or in more webby style: AudioTransferObject.
OK if you are using header guards, then indeed the issue is likely to come from putting the object files in single directory. Two possible workarounds:
- move one of the headers to a separate .pri file - not sure if that causes build layout to change, but it is the easiest to check
- separate part of your project into a subproject (qmake's
TEMPLATE = subdirs), possibly as a library, and then include with your main app
object_parallel_to_sourcework right out of the box? From what I understand it's supposed to mimic the project directory structure when compiling and should thereby eliminate duplicate
.obj-files in the same directory. The problem is the almost instant error mentioned in the first post.
But shouldn't object_parallel_to_source work right out of the box?
No idea - if this option exists, it is not documented in qmake manual.
What version of Qt are you using to build your project ?
@SGaist I am using the current version (5.11.2) and the up-to-date QtCreator. I am building my project with
object_parallel_to_sourceenabled and it only works when I disable shadow build. I would love to use the feature in combination with shadow build.
(Personal opinion): Identical filenames in a project lead to problems sooner or later.
Instead of working around the problem, you should just avoid these different classes with same name. @sierdzio already gave examples how structure things better.
Even if you get it running now, it may break anytime, leading to addional time to fix these things. Better invest that time into development.
@aha_1980 You probably right. I am still learning C++ and I am currently trying to figure out the best way to organize my source files. Currently project structure looks something like this:
│ ├── namespace1_base.cpp
│ ├── namespace1_derived1.cpp
│ └── namespace1_derived2.cpp
│ ├── namespace2_base.cpp
│ ├── namespace2_derived3.cpp
│ └── namespace2_derived4.cpp
I usually use a
Baseclass which implements functionality the classes inside the namespace need. It just seems odd not to call the source file
base.cppbecause I can't use duplicate files names.
Is there any way to fix that and use multiple files with the same name inside my Qt project? Does it perhaps have something to do with the fact, that I am compiling into a bin dir inside my repository?
Not with shadow building, no. When you generate a bunch of object files in the same directory, the object is named after the source file. So the identically named source's object file will overwrite the previous one (in order of building); it may even happen only partially if you're building on multiple cores.
It's a known pitfall and you should name your files better as people have already suggested.