包含库的C ++运行文件失败而没有编译错误(CMake / CLion) [英] C++ running file with included library failes without compiling error (CMake / CLion)
问题描述
我遇到了以下问题;我的超级库名为mylib:看起来像这样:
I got the following problem; I have my super library called mylib: it looks like this:
我的项目文件夹称为库...
mylib.hpp
My project folder is called library...
mylib.hpp
namespace XYZ {
#ifndef LIBRARY_MYLIB_HPP
#define LIBRARY_MYLIB_HPP
int add(int, int);
#endif //LIBRARY_MYLIB_HPP
}
mylib.cpp
#include "mylib.hpp"
namespace XYZ {
int add(int a, int b) {
return a + b;
}
}
它们在同一个目录中。
They are in the same directory.
我使用CMake和以下CMakeLists.txt构建它
I Build it using CMake with the following CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(library)
add_library(library SHARED mylib.cpp)
建筑物输出:
[ 50%] Building CXX object CMakeFiles/library.dir/mylib.cpp.obj
[100%] Linking CXX shared library liblibrary.dll
[100%] Built target library
这可以正常工作,这给了我一个* .dll文件。
我现在尝试在其他项目中使用该库
This works, this gives me a *.dll file like it's supposed to.
I now try to use this library in my other project
我复制了.hpp文件放入项目位置。
我的main.cpp看起来像这样:
I copied the .hpp file into the project location. my main.cpp looks like this:
main.cpp
#include <iostream>
#include "mylib.hpp"
int main() {
std::cout << "Hello";
std::cout << XYZ::add(5, 7) << std::endl;
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.6)
project(uselib)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(uselib ${SOURCE_FILES})
target_link_libraries(uselib C:/User/........./liblibrary.dll)
建筑输出:
[ 50%] Building CXX object CMakeFiles/uselib.dir/main.cpp.obj
[100%] Linking CXX executable uselib.exe
[100%] Built target uselib
有效。好了,现在解决我的问题。
Works. Well now to my problem.
一旦我尝试启动它,它就会崩溃,并显示以下退出代码:
Once I try to start it, it crashes with the following exit code:
C:\Users\......\uselib.exe
Process finished with exit code -1073741515 (0xC0000135)
当我对方法调用进行注释时,它就像一个符咒。
When I comment the method call out, it works like a charm.
我在这里呆了几个小时,有什么帮助吗?
I've been stuck here for hours, any help?
推荐答案
退出代码- 1073741515(0xC0000135)
为STATUS_DLL_NOT_FOUND。这表明在运行时该程序无法使用该dll。在Windows中,dll的搜索路径如下:
Exit code -1073741515 (0xC0000135)
is STATUS_DLL_NOT_FOUND. This would indicate that the dll isn't available to the program at runtime. In windows, the search path for a dll is as follows:
- 当前进程的可执行文件所在的目录
。 - 当前目录。
- Windows系统目录。
GetSystemDirectory
函数
检索此目录的路径。 - Windows目录。
GetWindowsDirectory
函数检索
此目录的路径。 - PATH环境变量中列出的目录。
- The directory where the executable for the current process is located.
- The current directory.
- The Windows system directory. The
GetSystemDirectory
function retrieves the path of this directory. - The Windows directory. The
GetWindowsDirectory
function retrieves the path of this directory. - The directories listed in the PATH environment variable.
验证liblibrary.dll位于可执行文件可以找到的位置。
Verify that liblibrary.dll is located in a place it can be found by the executable.
完成此操作后,您可能希望添加一个宏来帮助您将dll复制到可执行文件目录中,作为构建的一部分。可以这样在cmake中完成:
Once you have done that, you may want to add a macro to help you copy the dll to the executable directory as part of your build. This can be done in cmake like so:
add_custom_command(TARGET uselib POST_BUILD # Adds a post-build event to uselib
COMMAND ${CMAKE_COMMAND} -E copy_if_different # which executes "cmake - E copy_if_different..."
"${PROJECT_SOURCE_DIR}/libs/liblibrary.dll" # this is in-file
$<TARGET_FILE_DIR:uselib>) # this is out-file path
这篇关于包含库的C ++运行文件失败而没有编译错误(CMake / CLion)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!