Visual Studio 2012:从链接中排除编译的文件 [英] Visual Studio 2012: exclude compiled file from linking

查看:102
本文介绍了Visual Studio 2012:从链接中排除编译的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的C语言项目,正在编译几个文件.这些文件将编译为.obj文件,然后链接在一起.到目前为止,到目前为止,我需要从链接中排除这些文件之一.

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.

我知道这很不寻常,但这正是我所需要的:编译文件但不将其链接到项目的输出文件.使用Visual Studio 2012.

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.

尽管我一直在寻找项目属性和vcxproj文件中的各个位置,但我不知道链接器在哪里获取要链接的文件.必须是编译器传递已编译文件的列表.是否可以自定义此构建阶段?

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?

在Visual Studio 2010中,很容易从链接中排除文件:默认情况下,所有已编译文件都存储为$(IntDir)%(FileName).obj.如果.obj文件存储在其他位置(在我的情况下仅为%(FileName).obj),则未链接该文件. VS2012似乎链接所有实际编译的文件,无论它们位于何处.

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.

如果我将文件名更改为%(FileName).obj.x,则没有任何变化,链接器仍将其链接.

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.

推荐答案

让我回答我自己的问题.解决此问题的方法有三种:

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

  1. 构建后事件编译文件.这样,Visual Studio对文件一无所知.失去了以标准方式构建它的许多优点.

  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.

按照@HansPassant的建议,单独的项目将编译文件.这不会阻止链接步骤,因此会生成不必要的文件,并且构建速度会变慢.就我而言,将文件移动到另一个项目是有问题的,因为它自然属于原始项目.

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.

我使用最初被忽略的预链接事件.我用链接的空文件重写了已编译的文件.这有点棘手,但对我来说可以接受.

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.

这篇关于Visual Studio 2012:从链接中排除编译的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆