2

I have a simple C language project with a few files being compiled. These are compiled to .obj files and then linked together. So far so good, now I need to exclude one of these files from linking.

I know this is quite unusual but this is exactly what I need: to compile a file but not link it to the project's output file. Using Visual Studio 2012.

I have no idea where the linker grabs the files to link although I've been looking to various places in project properties and the vcxproj file. It must be the compiler that passes the list of the compiled files. Is it possible to customize this building phase?

In Visual Studio 2010, it was easy to exclude a file from linking: By default, all the compiled files were stored as $(IntDir)%(FileName).obj. If an .obj file was stored elsewhere (in my case it is just %(FileName).obj), it was not linked. VS2012 seem to link all files actually compiled, regardless of their location.

If I change the file name to %(FileName).obj.x, nothing changes, the linker still links it.

I know I could compile the file as a Post-Build Event but I'd like to have the file compiled as a part of the standard building process.

Tunaki
  • 132,869
  • 46
  • 340
  • 423
MazeGen
  • 180
  • 3
  • 14
  • IDE designers just don't spend a lot of time on adding whistles that make no sense. There's no perceivable reason to compile the file if you are not going to use the link the .obj file. If there is one then you shouldn't have kept it a secret. Right-click the .c file and select "Exclude from build". – Hans Passant Jan 28 '14 at 17:32
  • @HansPassant The reason is that I don't want to link the file, I need to load it later as an external COFF file by the project itself. Unusual but it does make sense in my specific case. Perhaps there is a way how to modify the list of files that is passed to the linker, who knows. – MazeGen Jan 28 '14 at 18:01
  • 1
    Just keep in a separate lib project. – Hans Passant Jan 28 '14 at 18:25
  • @HansPassant in addition to the OP use case I need this for a legacy file that I want to ensure continues to compile but is not included in the link because later on I may need to extract code from the file. Putting it into a separate project is unacceptable because the project settings (eg preprocessor defines) affect compilation of the file. Additionally, an "exclude from link" would be helpful for verifying that headers compile in the absence of any other definitions. – Bowie Owens Apr 21 '17 at 02:40

1 Answers1

1

Let me answer my own question. There are three solutions to this problem:

  1. Post-Build Event compiles the file. This way, Visual Studio knows nothing about the file; many advantages of building it the standard way are lost.

  2. As @HansPassant suggested, separate project compiles the file. This doesn't prevent the linking step so unnecessary file is generated and the build is a bit slower. And in my case, moving the file to another project is questionable as it naturally belongs to the original project.

  3. I use the Pre-Link Event that I overlooked at first. I rewrite the compiled file with an empty one that is linked instead. It's a bit tricky but acceptable for me.

MazeGen
  • 180
  • 3
  • 14
  • Can you detail how you did (3)? – Bowie Owens Apr 21 '17 at 02:41
  • 1
    @BowieOwens If I remember well, I had an extra source file that compiled to an empty object file. I deleted the compiled file and renamed the empty one at the pre-link time. The linker was fine with it. (EDIT: It was something like "del x.obj && ren y.obj x.obj" right away as the Pre-Link event command) – MazeGen Apr 21 '17 at 09:47