在 Visual Studio 中使用 CMake 将静态 libconfig 库链接到示例程序

问题描述 投票:0回答:0

我正在尝试在 Visual Studio 2019 中使用带有 CMake 的静态库

libconfig++_d.lib
编译示例程序。

我能够使用 CMake 正确构建和链接我的示例程序。但是,当我去运行可执行文件时,出现错误:

LIB_LINK.exe - 系统错误 代码执行无法继续,因为未找到 libconfig++_d.dll。重新安装程序可能会解决此问题。

在CMake文件中,我链接了一个静态库

.lib
。为什么可执行文件要寻找动态链接库
.dll
?我只需要静态库文件和相关的头文件就可以正确链接吗?

这是我的 CMake 文件:

cmake_minimum_required(VERSION 3.10)

project(LIB_LINK)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

add_executable(LIB_LINK "example1.cpp")

target_compile_definitions(LIB_LINK PUBLIC LIBCONFIGXX_STATIC)
target_include_directories(LIB_LINK PUBLIC ${CMAKE_SOURCE_DIR}/libconfig/lib)
target_link_libraries(LIB_LINK ${CMAKE_SOURCE_DIR}/libconfig/libconfig++_d.lib)

example1.cpp:

#include <iostream>
#include <iomanip>
#include <cstdlib>
#include "libconfig.h++"

using namespace std;
using namespace libconfig;

// This example reads the configuration file 'example.cfg'
int main(int argc, char** argv)
{
    (void)argc;
    (void)argv;

    Config cfg;

    // Read the file. If there is an error, report it and exit.
    try
    {
        cfg.readFile("example.cfg");
    }
    catch (const FileIOException& fioex)
    {
        std::cerr << "I/O error while reading file." << std::endl;
        return(EXIT_FAILURE);
    }

    return(EXIT_SUCCESS);
}

这是调试输出:

'LIB_LINK.exe' (Win32): Loaded 'C:\Windows\SysWOW64\ntdll.dll'. 
'LIB_LINK.exe' (Win32): Loaded 'C:\Windows\SysWOW64\kernel32.dll'. 
'LIB_LINK.exe' (Win32): Loaded 'C:\Windows\SysWOW64\KernelBase.dll'. 
The thread 0x329c has exited with code -1073741515 (0xc0000135).
The thread 0x62d8 has exited with code -1073741515 (0xc0000135).
The program '[18412] LIB_LINK.exe' has exited with code -1073741515 
(0xc0000135) 'A dependent dll was not found'.
visual-c++ cmake static-libraries static-linking libconfig
© www.soinside.com 2019 - 2024. All rights reserved.