C++ running file with included library failes with

2020-04-30 01:43发布

I got the following problem; I have my super library called mylib: it looks like this:

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.

I Build it using CMake with the following CMakeLists.txt

cmake_minimum_required(VERSION 3.6)
project(library)

add_library(library SHARED mylib.cpp)

Building Output:

[ 50%] Building CXX object CMakeFiles/library.dir/mylib.cpp.obj
[100%] Linking CXX shared library liblibrary.dll
[100%] Built target library

This works, this gives me a *.dll file like it's supposed to.


I now try to use this library in my other project

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)

Building output:

[ 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?

标签: c++ cmake clion
1条回答
家丑人穷心不美
2楼-- · 2020-04-30 01:51

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:

  1. The directory where the executable for the current process is located.
  2. The current directory.
  3. The Windows system directory. The GetSystemDirectory function retrieves the path of this directory.
  4. The Windows directory. The GetWindowsDirectory function retrieves the path of this directory.
  5. The directories listed in the PATH environment variable.

Verify that liblibrary.dll is located in a place it can be found by the executable.

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
查看更多
登录 后发表回答