gpt4 book ai didi

c++ - 如何从静态 C 和 C++ 库创建共享 C 库?

转载 作者:太空狗 更新时间:2023-10-29 20:33:02 27 4
gpt4 key购买 nike

总结

我正在将基于 c/c++ 的静态库集成到基于 c 的共享库中(在 linux 上使用 cmake)。

我有这样的错误:libmylib.so: undefined reference to `std::ios_base::Init::Init()'

该问题与 c++ 的使用及其与共享库的链接密切相关。如果我避免使用共享库(甚至是另一个静态库),则不会发生错误。因为我的项目我无法避免使用静态库的共享库。

如何从 c/c++ 静态库和新的 c 源代码生成正确共享的库?

PD:很抱歉问了这么长的问题,我发布代码是为了让您了解我的问题的背景。

C++代码

cppexample.hpp

typedef struct cpp_api
{
int (*func_ptr)(const char *, int);
} cpp_api;

#ifdef __cplusplus
extern "C" {
#endif

const cpp_api* getApi();

#ifdef __cplusplus
}
#endif

cpp示例.cpp

int apiFunc(const char *strc, int value)
{
std::string str(strc);
std::cout << "Api call: " << str << std::endl;
return value;
}

static const cpp_api libapi =
{
&apiFunc,
};

extern "C"
{
const cpp_api* getApi()
{
return &libapi;
}
}

C代码

例子.h

void doSomething();

例子.c

#include "example.h"
#include "cpp_lib/cppexample.hpp"

void doSomething()
{
const cpp_api *api = getApi();
int result = api->func_ptr("hello!", 12);
}

主要API.h

void callDoSomething();

mainApi.c

#include "mainApi.h"
#include "mix_lib/example.h"

void callDoSomething()
{
doSomething();
}

主.c

#include "mainApi.h"
int main(int argc, const char* argv[])
{
callDoSomething();
return 0;
}

CMake代码

图书馆基因

cmake_minimum_required(VERSION 3.5)
set(CMAKE_BUILD_TYPE "Release") #Not debug
set(THIS MAINLIB) #project name
project(${THIS} C CXX) #For c and c++
set(CMAKE_C_STANDARD 99) #c99
set(CMAKE_CXX_STANDARD 11) #c++11
set(CMAKE_POSITION_INDEPENDENT_CODE ON) #fPIC

set(MAIN_LIB_ROOT "${CMAKE_CURRENT_SOURCE_DIR}")

###### MAIN LIB ######
include_directories(${MAIN_LIB_ROOT} ${MAIN_LIB_ROOT}/main)
list(APPEND MAIN_LIB_SOURCES
"${MAIN_LIB_ROOT}/main/mainApi.h"
"${MAIN_LIB_ROOT}/main/mainApi.c")
set(MAIN_LIB_NAME mainlib)
add_library(${MAIN_LIB_NAME} STATIC ${MAIN_LIB_SOURCES})
include("${MAIN_LIB_ROOT}/cpp_lib/cpp_lib.cmake")
include("${MAIN_LIB_ROOT}/mix_lib/mix_lib.cmake")
setup_cpp_lib()
setup_mix_lib()

###### MAIN EXECUTABLE ######
add_executable(${THIS} "${MAIN_LIB_ROOT}/main.c")
target_link_libraries(${THIS} PUBLIC ${MAIN_LIB_NAME})
list(APPEND MAIN_INSTALL_BINS
${THIS})

setup_cpp_lib()

list(APPEND CPP_LIB_SOURCES
"${MAIN_LIB_ROOT}/cpp_lib/cppexample.hpp"
"${MAIN_LIB_ROOT}/cpp_lib/cppexample.cpp")

function(setup_cpp_lib)
add_library(cpplib OBJECT ${CPP_LIB_SOURCES})
target_sources(${MAIN_LIB_NAME} PRIVATE $<TARGET_OBJECTS:cpplib>)
endfunction()

setup_mix_lib()

list(APPEND MIX_LIB_SOURCES
"${MAIN_LIB_ROOT}/mix_lib/example.h"
"${MAIN_LIB_ROOT}/mix_lib/example.c")

function(setup_mix_lib)
add_library(mixlib OBJECT ${MIX_LIB_SOURCES})
target_sources(${MAIN_LIB_NAME} PRIVATE $<TARGET_OBJECTS:mixlib>)
endfunction()

到此为止,libmainlib.a 已创建。我将该库和 mainApi.hmain.c 复制到另一个项目并发生错误

Cmake 出现错误

cmake_minimum_required(VERSION 3.5)
set(CMAKE_BUILD_TYPE "Release") #Not debug
set(THIS MYPROJECT) #project name
project(${THIS} C CXX) #For c only
set(CMAKE_C_STANDARD 99) #c99
set(CMAKE_CXX_STANDARD 11) #c++11
set(MAIN_LIB_ROOT "${CMAKE_CURRENT_SOURCE_DIR}") #current cmakelists path

include_directories(${MAIN_LIB_ROOT}/includes) #includes for library headers
add_library(mylib SHARED "${MAIN_LIB_ROOT}/main.c")
add_library(mainlib STATIC IMPORTED)
set_property(TARGET mainlib PROPERTY IMPORTED_LOCATION ${MAIN_LIB_ROOT}/lib/libmainlib.a)
target_link_libraries(mylib mainlib)
add_executable(${THIS} "${MAIN_LIB_ROOT}/main.c")
target_link_libraries(${THIS} mylib)

list(APPEND MAIN_INSTALL_BINS ${THIS})
install(TARGETS ${MAIN_INSTALL_BINS} DESTINATION "${CMAKE_INSTALL_PREFIX}")

错误

libmylib.so: undefined reference to `std::ios_base::Init::Init()'
libmylib.so: undefined reference to `std::string::_Rep::_M_destroy(std::allocator<char> const&)'
libmylib.so: undefined reference to `std::string::_Rep::_S_empty_rep_storage'
libmylib.so: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, long)'
collect2: error: ld returned 1 exit status
make[2]: *** [MYPROJECT] Error 1
make[1]: *** [CMakeFiles/MYPROJECT.dir/all] Error 2
make: *** [all] Error 2

===编辑===

I have a static library called libmainlib.a (simulates a complex library that was modified injecting c++ code). That library is used to generate a shared library called libmylib.so (simulates another complex library that I didn't modify but uses the static library).

最佳答案

您得到了 undefined reference ,因为 mylib 库被链接为 C 对象。但由于静态库 libmainlib.a 是 C++ 库,因此它需要 C++ 链接。有关此类 undefined reference 来源的更多信息,请提问:undefined reference to `std::ios_base::Init::Init()' .

这种不正确的链接是因为你的第二个代码中的库 mainlibSTATIC IMPORTED (两个关键字都很重要)而 CMake 不是了解该库的实际语言。

您需要提示 CMake 给定的库实际上是一个 C++ 库并且需要 C++ 链接:

set_property(TARGET mainlib PROPERTY IMPORTED_LINK_INTERFACE_LANGUAGES CXX)

您的第一个代码工作正常,因为它的 mainlib 库虽然是 STATIC,但不是 IMPORTED 并且 CMake 知道它的来源。由于源 cppexample.cpp 绝对是 C++ 源,CMake 将 mainlib 库视为 C++。

关于c++ - 如何从静态 C 和 C++ 库创建共享 C 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57719290/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com