I have seen a similar question but I cannot make sense by that accepted answer.
my project structure is like this
├── CMakeLists.txt
├── main.cpp
└── plugins
├── CMakeLists.txt
├── plugin.cpp
└── plugin.h
The content of CMakeLists.txt
is like this:
# top-level CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 3.22)
PROJECT(example)
add_subdirectory(./plugins/)
add_executable(main main.cpp)
target_include_directories(main PUBLIC ./plugins/)
target_link_libraries(main -lplugin)
CMakeLists.txt in folder of ./plugins/
add_library(plugin SHARED plugin.cpp)
And the code is like this:
// main.cpp
#include <iostream>
#include "plugin.h"
int call() {
using namespace std;
cout << "call\n";
func();
}
int main() {
call();
return 0;
}
// plugins/plugin.cpp
#include <iostream>
void func() {
using namespace std;
cout << "plugin\n";
}
// plugins/plugin.h
void func();
When I compile this, I got the error that
make[2]: *** No rule to make target 'plugins/libplugin.so', needed by 'main'. Stop.
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
Would you please tell me how to make this work?
If you change your target_link_libraries
call to the following:
target_link_libraries(main PUBLIC plugin)
CMake will:
main
target to the plugin
target.main
such that it knows where to find libplugin.so