1

I've read a number of posts concerning similar issues but I still can't find the solution to the following problem.

I have two CLion (OS Windows) projects mylib and myexe which reside in separate directories at the same level. mylib consists of two files: library.h

void hello();

and library.cpp

#include "library.h"
#include <iostream>

void hello() {
    std::cout << "Hello, World!" << std::endl;
}

The CMakeLists.txt for mylib is the following:

cmake_minimum_required(VERSION 3.16)
project(mylib)

set(CMAKE_CXX_STANDARD 14)

add_library(mylib SHARED library.cpp library.h)

Next, the project myexe consists of one file main.cpp

#include "../mylib/library.h"

int main() {
    hello();
    return 0;
}

with the following CMakeLists.txt file

cmake_minimum_required(VERSION 3.16)
project(myexe)

set(CMAKE_CXX_STANDARD 14)

add_executable(myexe main.cpp)

find_library(RESULT mylib "d:/src/test/mylib/cmake-build-debug")
target_link_libraries(myexe "${RESULT}")

Both projects are built without errors. But when I run myexe, the "Hello, world" isn't printed, and I'm getting the following:

Process finished with exit code -1073741515 (0xC0000135)

Please help me, how to solve this issue and link DLL correctly.

kudral
  • 71
  • 1
  • 1
  • 7
  • 1
    This error means that you `myexe.exe` tries to load `mylib.dll` at start-up. Thus, it's linked correctly. Please, check whether `myexe.exe` and `mylib.dll` are built into the same directory (or copy `mylib.dll` into the folder of `myexe.exe` afterwards). Btw. In `CMake`, you can force that all binaries are built into the same output directory to prevent such issues in the future. – Scheff's Cat Jan 01 '21 at 11:08
  • 1
    FYI: [SO: How do I make CMake output into a 'bin' dir?](https://stackoverflow.com/a/6595001/7478597) – Scheff's Cat Jan 01 '21 at 11:10
  • 1
    FYI: [microsoft.com: 2.3.1 NTSTATUS Values](https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/596a1078-e883-4972-9bbc-49e60bebca55) (Please, do a text search on this site for `135`. I couldn't find anything in the HTML code to link to it directly.) – Scheff's Cat Jan 01 '21 at 11:17

1 Answers1

1

As @Scheff suggested, looking at How do I make CMake output into a 'bin' dir?, I just added theses three lines to the CMakeLists.txt files of both projects myexe and mylib:

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "../../bin")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "../../bin")
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "../../bin")

where "../../bin" is the desired directory to store .dll and .exe.

kudral
  • 71
  • 1
  • 1
  • 7
  • You should not override CMake's global build layout. Instead, use the [install()](https://cmake.org/cmake/help/latest/command/install.html) command to copy files to a final location, or use a `POST_BUILD` custom command to copy DLLs next to the exe. – alexchandel Mar 01 '23 at 17:36