我目前尝试将外部动态链接库(dll)添加到我的 cmake 项目中。我有 .dll 和 .lib 文件。我花了很多时间自己寻找答案,但我没有找到解决方案。包含有效,但是当我尝试编译最小解决方案时总是会出现链接错误。CMAKE 在配置和生成并找到库时不会抛出任何错误。我希望有人以前遇到过问题并认识到我的错误
我还构建了自己的查找模块,看起来像这样
find_path(LLT_INCLUDE_DIR InterfaceLLT_2.h HINTS ${LLT_DIR})
find_library(LLT_LIBRARY NAMES LLT HINTS ${LLT_DIR} )
set(LLT_LIBRARIES ${LLT_LIBRARY} )
set(LLT_INCLUDE_DIRS ${LLT_INCLUDE_DIR} )
include(FindPackageHandleStandardArgs)
# handle the QUIETLY and REQUIRED arguments and set LIBXML2_FOUND to TRUE
# if all listed variables are TRUE
find_package_handle_standard_args(LLT DEFAULT_MSG LLT_LIBRARY LLT_INCLUDE_DIR)
mark_as_advanced(LLT_INCLUDE_DIR LLT_LIBRARY )
对应的 CMakeLists.txt 看起来像这样
PROJECT(TestProg)
cmake_minimum_required(VERSION 2.8)
FIND_PACKAGE(LLT REQUIRED)
INCLUDE_DIRECTORIES(${LLT_INCLUDE_DIR})
SET(TestProg_SOURCES
main.cpp
)
SET(TestProg_HEADERS
)
add_executable(TestProg ${TestProg_SOURCES} ${TestProg_HEADERS})
target_link_libraries(TestProg ${LLT_LIBRARIES} )
这是CMAKE输出
The C compiler identification is MSVC 16.0.40219.1
The CXX compiler identification is MSVC 16.0.40219.1
Check for working C compiler using: Visual Studio 10
Check for working C compiler using: Visual Studio 10 -- works
Detecting C compiler ABI info
Detecting C compiler ABI info - done
Check for working CXX compiler using: Visual Studio 10
Check for working CXX compiler using: Visual Studio 10 -- works
Detecting CXX compiler ABI info
Detecting CXX compiler ABI info - done
Found LLT: C:/XXX/LLT.lib
Configuring done
Generating done
这是代码片段。我真的不知道您对链接命令的含义是什么,还是我忘记了代码中的某些内容?
#include <iostream>
#include "InterfaceLLT_2.h"
using namespace std;
int main()
{
bool bLoadError;
CInterfaceLLT* m_pLLT;
m_pLLT = new CInterfaceLLT("LLT.dll", &bLoadError);
cout << "Hello World!" << endl;
return 0;
}