【问题标题】:Maintaining common source code for 32/64 bit build and use the correct libs维护 32/64 位构建的通用源代码并使用正确的库
【发布时间】:2015-10-21 10:57:25
【问题描述】:

环境:Visual Studio 2008 Professional

我们正在将 32 位 Windows 应用程序移植到 64 位。我们的应用程序使用了许多 Microsoft dll,例如 htmlhelp.dll。为此,我们将他们的导入库添加到项目中。

现在我们有了 32 位和 64 位版本的 htmlhelp.lib(令人惊讶的是它们都具有相同的名称)。为什么MS给他们取了同样的名字。应用程序如何为当前构建平台选择正确的 .lib。

寻找处理此类情况的一般准则。

【问题讨论】:

  • 将库放入每个架构的文件夹中(例如 lib32\、lib64)。然后为每个构建设置库包含路径,或者使用完整路径将每个库添加到项目中。

标签: c++ visual-studio visual-c++ 32bit-64bit


【解决方案1】:

将它们放在不同的目录中:How to use the correct unmanaged DLL file according CPU architecture? (32 / 64 bits)

SetDllDirectory 函数会影响所有后续调用 LoadLibrary 和 LoadLibraryEx 函数。它还有效地禁用 搜索指定目录时的安全 DLL 搜索模式 路径。

调用SetDllDirectory后,标准DLL搜索路径为:

The directory from which the application loaded.
The directory specified by the lpPathName parameter.
The system directory. Use the GetSystemDirectory function to get the path of this directory. The name of this directory is System32.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched. The name of this directory

是系统。 Windows 目录。使用 GetWindowsDirectory 函数获取该目录的路径。 PATH 环境变量中列出的目录。

每次调用 SetDllDirectory 函数时,它都会替换 在前面的 SetDllDirectory 调用中指定的目录。指定 多个目录,使用 AddDllDirectory 函数并调用 LoadLibraryEx 与 LOAD_LIBRARY_SEARCH_USER_DIRS。

恢复到 LoadLibrary 使用的标准搜索路径和 LoadLibraryEx,使用 NULL 调用 SetDllDirectory。这也恢复了安全 基于 SafeDllSearchMode 注册表值的 DLL 搜索模式。

要编译使用此函数的应用程序,请定义 _WIN32_WINNT 为 0x0502 或更高版本。有关详细信息,请参阅使用 Windows 标题。

我对跨平台构建脚本使用相同的技术:

# OS-dependant tools and files
ifeq ($(OS), Windows_NT)
    ARCH = win
else
    ARCH = linux
endif

TMP := tmp_$(ARCH)
LIB := lib_$(ARCH)
BIN := bin_$(ARCH)

在 VisualStudio 中,为每个架构创建一个调试和发布构建配置,并相应地指向工作目录(放置 dll 的位置)。

【讨论】:

    【解决方案2】:

    为此类库(如 lib)创建公共文件夹,其中包含两个名为构建目标的子文件夹(x86 和 x64)。然后添加

    项目属性 -> 链接器 -> 常规

    其他库目录

    $(平台目标)

    适用于所有配置。 如果您有不同的 Debug 和 Release 版本,也可以创建这样的子文件夹,例如 lib\x64\Debug,并添加到您的目录中

    $(平台目标)\$(配置)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-12-01
      • 2011-04-20
      • 1970-01-01
      • 2014-04-10
      • 2013-11-14
      • 2023-03-25
      • 2017-02-19
      相关资源
      最近更新 更多