尝试将静态链接到共享库-获取未定义的引用

时间:2019-02-04 14:36:20

标签: cmake static-libraries

我的目标是通过使用cmake 3.4从共享库中的静态库中使用api。

静态库的非常简单的代码

// Header
class Structure 
{
public:
    int sum( int a, int b );
    int mul( int a, int b ) { return a * b; }
};
// Source
#include "Include/Structure.hpp"

int Structure::sum( int a, int b ) { 
    return a + b;
}

..和共享类来源中的用法

Structure* s = new Structure();
s->sum(3, 4);
s->mul(3, 4);

用于静态库的Cmake文件:

project(MyStaticLibrary)
include_directories(Include)
file(GLOB SOURCES "*.cpp")
add_library(${CMAKE_PROJECT_NAME} STATIC ${SOURCES})
set_target_properties( ${CMAKE_PROJECT_NAME} PROPERTIES COMPILE_FLAGS "-fPIC")

共享库的Cmake文件:

include_directories( ${static_lib_src_DIR}/Include )
add_subdirectory( ${static_lib_src_DIR} ${static_lib_build_DIR} )
add_library( MyStaticLibrary STATIC ${static_lib_src_DIR})
set_target_properties( MyStaticLibrary PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties( MyStaticLibrary PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/...../libMyStaticLibrary.a )

add_library( native-lib SHARED native-lib.cpp)
add_dependencies(native-lib MyStaticLibrary)
target_link_libraries( native-lib MyStaticLibrary )

链接共享库时出现错误消息:未定义对'Structure :: sum(int,int)'的引用

0 个答案:

没有答案