您可以在加载的项目上制作VC ++解决方案集预处理器#defines吗? [英] Can you make a VC++ Solution set preprocessor #defines on loaded projects?

查看:57
本文介绍了您可以在加载的项目上制作VC ++解决方案集预处理器#defines吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个支持#define的库,用于控制其构建方式。但是,该库可以由需要不同版本的多个EXE项目使用。我可以使app / EXE项目在构建时设置要由库使用的#define,还是在解决方案中进行设置?

I have a library which supports a #define to control how it's built. However the library can be used by multiple EXE projects which want different versions. Can I make the app/EXE project set the #define to be used by the library when built, or set it in the solution?

我能想到的唯一其他选择的工作是在库项目上创建单独的构建配置,但这很快就会失去控制。这对于unicode / non-unicode构建是很常见的,但是最终您会乘以每个组合的配置数量。

The only other option I can think of is creating a separate build-configuration on the library project but that would quickly get out of control. That's common for e.g unicode/non-unicode builds but then you'd end up multiplying the number of configurations for every combination.

推荐答案

以下方法假定每个.EXE / app(使用此库)都有自己的Visual Studio解决方案。

The following approach assumes that every .EXE/app (which uses this library) has its own Visual Studio Solution.

您确实可以控制该库,对吗?步骤1-3将对其项目文件进行更改,步骤4将一个文件添加到库源代码中。

You do have control over the library, right? Steps 1-3 will make changes to its project file, and step 4 adds a file to the library source code.


  1. Set项目属性> C / C ++>高级>强制包含到 mylibrary_solution_defines.h

编辑项目属性> C / C ++>常规>其他包含目录,将 $(SolutionDir); 放在目录列表的开头。

Edit Project Properties > C/C++ > General > Additional Include Directories to put $(SolutionDir); at the beginning of the list of directories.

将项目属性>常规>输出目录和中间目录都设置为与解决方案目录相关的目录。也许 $(SolutionDir)$(ProjectName)\ $(Configuration)?您想确保为使用该库的每个解决方案重新构建该库;

Set both Project Properties > General > Output Directory and Intermediate Directory to something relative to the solution directory. Perhaps $(SolutionDir)$(ProjectName)\$(Configuration)? You want to make sure the library gets rebuilt for every solution that uses it; there shouldn't be any sharing of .lib or .obj files.

创建一个空的虚拟头文件,名为 mylibrary_solution_defines.h。 并将其放入您的库源代码中,这样 #include mylibrary_solution_defines.h 永远不会失败。

Create an empty, dummy header file called mylibrary_solution_defines.h and put it in your library source code so a #include "mylibrary_solution_defines.h" will never fail.

在每个应用程序/ EXE解决方案中-假设您对使用此库的每个应用程序都有不同的解决方案,否则整个计划将失败-创建 mylibrary_solution_defines.h 文件,其中包含#define。

In every app/EXE solution -- assuming you have different solutions for each app that uses this library, otherwise this whole plan will fail -- create a mylibrary_solution_defines.h file with your #defines in it.

您知道发生了什么吗?每个库源文件都隐式地 #include s mylibrary_solution_defines.h ,并且优先从解决方案目录获取该文件。 。因此,每个解决方案的文件都可能不同。因此,如果您的解决方案 ConsoleModeInterfaceProgram.sln 需要使用 #define TEXTONLY 1 构建的库,则将该行放入 mylibrary_solution_defines.h ConsoleModeInterfaceProgram.sln 在同一目录中。

Do you see what's happening? Every library source file implicitly #includes "mylibrary_solution_defines.h", and it preferentially gets that file from the solution directory. So that file can be different for every solution. So if your solution ConsoleModeInterfaceProgram.sln needs the library built with #define TEXTONLY 1, put that line into the mylibrary_solution_defines.h that's in the same directory as ConsoleModeInterfaceProgram.sln.

这篇关于您可以在加载的项目上制作VC ++解决方案集预处理器#defines吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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