我正在使用 g++/KDevelop/CMake(3.16.3) 进行构建。
在我放之前cmake_minimum_required( VERSION 3.0.0 )
在我的 CMakeLists.txt 的第一行中,一切正常,现在我需要将其更改为cmake_minimum_required( VERSION 3.4.0 )
因为其他要求。但是有了这个我可以NOT使用新的主程序加载库。
我的程序是一些小的 *.so 库作为“模块”,并且是dlopen
由主程序。
头文件.hpp
#pragma once
int GetNumber();
小模块.cpp
#include "header.hpp"
extern "C" int newStrategy() {
return GetNumber();
};
加载器.cpp
#include <dlfcn.h>
#include "header.hpp"
int GetNumber() {
return 123;
};
bool anyError( void* pFunc ) {
const char* err_msg = dlerror();
if( err_msg != nullptr ) {
lg_erro << "Error on loading:" << err_msg;
return true;
}
if( pFunc == nullptr ) {
lg_erro << "Error on loading:NULL ptr";
return true;
}
return false;
};
using StrategyMaker_f = auto( * )()->int;
int main( int argc, char** args ) {
void* pFunc = args; // any ptr not NULL
// no err left before
if( anyError( pFunc ) )
exit( EXIT_FAILURE );
auto lib_handle = dlopen( "/path/to/modules/littleModule.so", RTLD_NOW );
if( anyError( lib_handle ) )
exit( EXIT_FAILURE );
pFunc = dlsym( lib_handle, "newStrategy" );
if( anyError( pFunc ) )
exit( EXIT_FAILURE );
auto _mk_func = reinterpret_cast<StrategyMaker_f>( pFunc );
lg_debg << "Success with:" << _mk_func();
exit( EXIT_SUCCESS );
};
CMakeLists.txt (loader.cpp)
cmake_minimum_required( VERSION 3.0.0 )
# cmake_minimum_required( VERSION 3.4.0 )
add_executable( loader Loader.cpp
common/log/Log2Console.cpp
common/misc/Converts.cpp
)
target_link_libraries( loader dl )
install( TARGETS loader RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} )
CMakeLists.txt (littleModule.cpp)
cmake_minimum_required( VERSION 3.0.0 )
# cmake_minimum_required( VERSION 3.4.0 ) still don't work
# add_library( littleModule SHARED littleModule.cpp )
# As @Tsyvarev's hint
add_library( littleModule MODULE littleModule.cpp )
set_target_properties( littleModule PROPERTIES
PREFIX ""
OUTPUT_NAME_RELEASE littleModule
OUTPUT_NAME_DEBUG littleModule
VERSION ${PROJECT_VERSION}
)
install( TARGETS littleModule LIBRARY DESTINATION strategies )
错误输出:
加载错误:未定义符号:_Z9GetNumberv