0

My program consists of one shared library and a main which links to it. Everything works fine until I split implementation and definition in my shared library.

|-CMakeList.txt
|-main.cpp
|lib
    |-CMakeList.txt
    |-myclass.cpp
    |-myclass.hpp

Shared library - CMakeList.txt

include_directories(include ${CMAKE_CURRENT_BINARY_DIR})
add_library(mylib SHARED myclass.hpp myclass.cpp)

Shared library - myclass.hpp

#include <cstdio>

class myclass
{
public:
    void msg();
};

Shared library - myclass.cpp

#include "myclass.hpp"

void myclass::msg() // works fine if inside .hpp
{
    printf("this is a test message");
}

Root - CMakeList.txt

cmake_minimum_required(VERSION 3.16)
project(test)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_BUILD_TYPE Debug)

add_subdirectory(lib)

add_executable(test main.cpp)

target_link_libraries(test mylib)

Root - main.cpp

#include "lib/myclass.hpp"
int main()
{
    myclass m{};
    m.msg();
    return 0;
}

I am using a CLion IDE on Windows. If I drop the dll's next to my .exe and run it from terminal, everything works fine too.

EDIT: When the dll's are placed in the same folder as .exe, the program works fine only when run from terminal; the IDE returns code 127. I don't know if it is a quirk of CLion and I need to tweak it somehow. The problem is that I can't debug through IDE.

Also, when the implementation of the myclass.msg() is in the header everything works fine in both IDE and terminal. But when I split implementation and definition I get the error described above.

  • 2
    "If I drop the dll's next to my .exe...everything works fine too". Which tells you EXACTLY what the problem is: your .exe has a dependent .dll that can't be found at runtime. The issue is "Windows DLL Search Order". Look [here](https://stackoverflow.com/a/6546427/421195) and [here](https://learn.microsoft.com/en-us/windows/win32/dlls/dynamic-link-library-search-order) – paulsm4 Aug 08 '20 at 06:12
  • @paulsm4 "Everything works fine until I split implementation and definition in my shared library." I added an edit to my post. Why do I get this problem *only* when the implementation is in .cpp AND I run it from the IDE. If I make it a header-only library, everything works fine. Also, I did place the dll's next to .exe to run the program through terminal – Daniil Shuraev Aug 09 '20 at 05:01

1 Answers1

0

as you are building with this structure

|-CMakeList.txt
|-main.cpp
|lib
    |-CMakeList.txt
    |-myclass.cpp
    |-myclass.hpp

your program is correctly linked with your dll

$ objdump -x test.exe | grep DLL
 vma:            Hint    Time      Forward  DLL       First
        DLL Name: cygwin1.dll
        DLL Name: KERNEL32.dll
        DLL Name: cygmylib.dll

but the shared lib is not in the same directory

$ ls -1F
cmake_install.cmake
CMakeCache.txt
CMakeFiles/
lib/
Makefile
test.exe*

$ ls -1F lib
cmake_install.cmake
CMakeFiles/
cygmylib.dll*
libmylib.dll.a
Makefile

so you can put all in the same directory, or you add the lib subdirectory to your IDE test path
As example from Cygwin Terminal

$ ./test.exe
D:/cygwin64/tmp/prova/build/test.exe: error while loading shared libraries: cygmylib.dll: cannot
open shared object file: No such file or directory

$ cp lib/cygmylib.dll .

$ ./test.exe
this is a test message
matzeri
  • 8,062
  • 2
  • 15
  • 16