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.