VisualStudio project with multiple sourcefiles of the same name?

前端 未结 3 963
执念已碎
执念已碎 2020-12-09 06:08

i have a largish C++ project, with source-files organised in multiple folders (on the filesystem).

in two of these folders, i have files with the same name. e.g.

相关标签:
3条回答
  • 2020-12-09 06:19

    You can set a file-specific project setting for one (or both) of the files that conflict and set the "Object File Name" property to:

    $(InputDir)\$(IntDir)\
    

    Just right-click the filename instead of the project name to set the property for that file only.

    For example, if you do that for \MyProject\foo\File.cpp then the object file for that source file will go to \MyProject\foo\Release\File.obj so it won't conflict with the object file for \MyProject\bar\File.cpp.

    The drawbacks to this are that it can clutter your source tree with compiler outputs (but hopefully there aren't too many), and - even worse - file-specific project settings tend to get forgotten/hidden since they're not called out in the IDE at all. If sometime down the road you (or someone else) needs to change things, it can be quite a mystery about why the build acts so strangely for particular files until someone screws around with it for a half a day until it dawns on them what's going on.

    I would personally prefer that a project-wide setting of $(InputDir)\$(IntDir)\ would cause object files to go to directories relative to the source file, but it actually doesn't work well at all as a project level setting. In that case VS still only sets the output directory once and it ends up being relative to the first source file in the list. Then the linker gets confused about where it should look for object files.

    0 讨论(0)
  • 2020-12-09 06:27

    Use

    $(IntDir)%(Directory)
    

    as "Object File Path".

    %(Directory) contains the absolute path of the file without the volume and without the filename itself. This solution should work at least in VS 2019 and can be applied to a project directly.

    0 讨论(0)
  • 2020-12-09 06:31

    Maybe you can set the project wide 'object file name' (Configurtion Properties->C/C++->Output Files) to

    $(IntDir)%(RelativeDir)
    

    which uses the relative source folder of the source files. Note the %, but this gets ugly if your source files are located out of your project directory, containing ..\

    0 讨论(0)
提交回复
热议问题