【发布时间】:2020-05-07 16:29:51
【问题描述】:
我正在努力使用使用 CMAKE 和 C++ 生成的 DLL。我能够构建库,包含它并构建目标项目,问题是当我运行目标构建时它会立即崩溃。 我的代码非常简单,我不知道我错过了什么。 DLL 是在单独的项目中使用 CMAKE 构建的。这是代码
DLL 项目:
CMakeLists
cmake_minimum_required(VERSION 3.5)
project(LibProj LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_definitions("-DBUILD_LIB")
file(GLOB
INCLUDE_FILES
baselibraryclass.h
)
file(GLOB
SOURCE_FILES
baselibraryclass.cpp
)
add_library(yourlib SHARED ${INCLUDE_FILES} ${SOURCE_FILES} )
baselibraryclass.h
#ifndef BASELIBRARYCLASS_H
#define BASELIBRARYCLASS_H
#ifdef BUILD_LIB
#define EXT_DLL __declspec(dllexport)
#else
#define EXT_DLL __declspec(dllimport)
#endif
#include <string>
class EXT_DLL BaseLibraryClass
{
public:
BaseLibraryClass();
};
#endif // BASELIBRARYCLASS_H
baselibraryclass.cpp
#include "baselibraryclass.h"
#include <iostream>
EXT_DLL BaseLibraryClass::BaseLibraryClass()
{
std::cout << "Hi from the library Class Object " << std::endl;
}
目标项目
CMakeLists
cmake_minimum_required(VERSION 3.5)
project(TargetProject LANGUAGES CXX)
#Including the path of the library header
include_directories(D:/TestingDLLNativeCpp/Library/include)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_executable(TargetProject main.cpp)
#The path of the built library is D:/TestingDLLNativeCpp/Library/Debug/yourlib.lib
target_link_libraries(TargetProject PRIVATE D:/TestingDLLNativeCpp/Library/Debug/yourlib.lib)
目标项目main.cpp
#include <iostream>
#include <baselibraryclass.h>
using namespace std;
int main()
{
BaseLibraryClass testObk;
return 0;
}
正如我上面写的,cmake 配置正确,编译器能够为这两个项目构建,但是目标可执行文件立即崩溃。 我在做什么错o.O??
感谢关注
【问题讨论】:
-
首先包含崩溃中的完整错误
-
Starting D:\TestingDLLNativeCpp\Target\Build\TargetProject.exe ... The program has unexpectedly finished. The process was ended forcefully. D:\TestingDLLNativeCpp\Target\Build\TargetProject.exe crashed.:/ -
我希望该错误与程序员有关,而不是一般的“程序已崩溃”错误。调试代码时,调试器检测到什么错误?
-
看起来像一个 Qt 错误。 stackoverflow.com/questions/53587138/…
-
DLL 的位置/目录是否在您的
PATH上?您有链接库的绝对路径,但您可能需要将 DLL 的目录添加到您的PATH。 DLL 类似于可执行文件:Windows 有一套关于如何查找 DLL 的规则,并且确实涉及PATH(如果是 Powershell,则为$env:PATH。)
标签: c++ dll cmake shared-libraries libraries