如何在MFC中包含2个不同版本的同名文件? [英] How to include files of same name of 2 different versions in MFC?

查看:90
本文介绍了如何在MFC中包含2个不同版本的同名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个要在同一软件的两个不同版本上运行的项目.
例如:
依赖项:Software v1.0和Software v2.0中存在file.h和file.lib.
funtion1(int a,int b,int c);在v1.0版中定义
funtion1(int a,int c);在v2.0版中定义

我希望通过选择正确版本的文件来编译代码中的各个功能来构建项目.

面临的问题:在项目选项中,我设置了库并包含了file.h和file.lib的路径.
它将为功能选择错误的文件版本(因为选择基于优先级)

请建议我一种应该可以构建项目的方式.
我仅为此项目使用静态库.

问候
Draj

Hi,
I have a project to build that should run on two different versions of same software.
Ex:
Dependencies: file.h and file.lib are present in Software v1.0 and Software v2.0.
funtion1( int a, int b, int c); is defined in Software v1.0
funtion1( int a, int c); is defined in Software v2.0

I want the project to build by picking the right version of the file for compiling respective functions in the code.

Issue faced: In project options, I set the library and include path of file.h and file.lib.
It will pick the wrong version of the file for the funtions (because, selection is based on precedence)

Please suggest me a way by which i should be able to build the project.
I am using static libraries only for this project.

Regards
Draj

推荐答案

Draj!

如果可以的话,我建议使用命名空间和相对路径.

Hi, Draj!

If you can, I''d suggest the use of namespaces, and relative paths.

namespace prod1
{
#include "../prod1/file.h"
}
namespace prod2
{
#include "../prod2/file.h"
}

int main(int argc, char **argv)
{
    prod1::function1(1, 2, 3);
    prod2::function1(4, 5);
}


如果.LIB文件冲突,此解决方案可能对您没有帮助:这取决于您的代码.
您可以将.lib文件包装在自己的源代码中:


If the .LIB files conflict, this solution may not help you: that depends on your code.
You can wrap the .lib files in your own source:

// a.h
int Function1V1(int a, int b, int c);

// a.cpp
#include "../v1/file.h"
#pragma comment( lib, "../v1/file.lib" ) 
int Function1V1(int a, int b, int c)
{
    return funtion1(a, b, c);
}

// b.h
int Function1V2(int a, int b);

// b.cpp
#include "../v2/file.h"
#pragma comment( lib, "../v2/file.lib" ) 

int Function1V2(int a, int b)
{
    return funtion1(a, b);
}




否则,假设这些产品中的每一个都提供DLL而不只是LIB,则必须使用后期绑定(LoadLibrary()GetProcAddress()).

如果只有静态库,则可以将它们各自包装在您自己的DLL中,重新命名封装的函数,然后在程序中引用您自己的DLL.

另请参见如何构建使用VC ++的两个包含的库具有相同符号(例如函数/变量)的图像(DLL/EXE) [




Otherwise, assuming each of these products provides a DLL and not only a LIB, you''ll have to use late binding (LoadLibrary() and GetProcAddress()).

If you only have static libraries, you can wrap each of them in a DLL of your own, renaming the encapsulated functions, and refer to your own DLLs in your program.

See also How to build an image (DLL/EXE) when two of its included libraries have the same symbol (say function/variable) using VC++[^]

Hope this helps,
Pablo.


您可以添加其他项目配置.假设您具有用于版本1的调试和发布版本的配置,请为其创建副本并在名称中指示版本2.然后,您可以使用不同的包含目录和库目录,也可以针对不同的库添加链接.

对于所有配置,还应该为版本添加其他定义(命令行选项).此定义可在源代码中用于编译版本依赖块或排除它们:
You may add additional project configurations. Assuming you have configurations for debug and release builds of version 1, create copies of them and indicate version 2 in the names. Then you can use different include and library directories or add link against different libraries.

For all configurations, you should also add an additional definition for the version (command line option). This definition can be used in the source to compile version depend blocks or exclude them:
#ifndef MY_APP_VERSION
#error MY_APP_VERSION not defined
#endif

// Include version specific header files
//  and link against version specific libraries
#if MY_APP_VERSION == 1
#include ".\v1.file.h"
#pragma comment(lib, ".\v1\my.lib")
#else
#include ".\v2.file.h"
#pragma comment(lib, ".\v2\my.lib")
#endif

// Call version specific function
#if MY_APP_VERSION == 1
function1(a, b, c);
#else
function1(a, b);
#endif


这篇关于如何在MFC中包含2个不同版本的同名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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