【问题标题】:how to link custom static libraries in cmakelist如何在 cmakelist 中链接自定义静态库
【发布时间】:2021-07-12 19:50:56
【问题描述】:

我正在使用 clion 开发一个 c++ 项目,使用 capstone,但我确实不太了解 cmake 语法 有我的项目树 enter image description here

在 main.cpp 中使用了一个函数(称为 a()),在 cstool.h 中声明并在 cstool.cpp 中实现

并且a()中用到了函数print_insn_detail_arm(),print_insn_detail_arm64(),print_insn_detail_x86(),

print_insn_detail_arm() implemented in cstool_arm.c

print_insn_detail_arm64() implemented in cstool_arm64.c

print_insn_detail_x86() implemented in cstool_x86.c
no header files for the three .c files

但是编译时出错

我认为libcs​​tool_arch.a没有链接到项目,但我不知道如何写正确的句子。

不确定这三个.c文件是否需要头文件

我觉得cmake的句序等方面可能有一些错误或者思路不完善,希望初学者指导和建议

【问题讨论】:

  • 欢迎来到 Stack Overflow。在这里,我们不喜欢只包含 code 或/和 错误消息images。相反,我们希望这些内容以 text 的形式包含在问题帖子中。请阅读How to Ask 并相应地更新您的问题。
  • CMake 对我来说看起来不错。由于您在没有头文件的情况下使用这些函数,我假设您对这些函数进行了一些外部/前向声明?也许参数类型与预期不匹配?
  • 您好!静态库与 CMake 的链接可以参考this 帖子。

标签: c++ cmake


【解决方案1】:

您在 CMake 前端做的一切都是正确的,但您没有考虑 C++ 名称修改。 具体来说,C++和C在print_insn_detail_arm()对应的符号名上存在分歧:

$ nm from_c.o
U _print_insn_detail_arm
$ nm from_cpp.o
U __Z21print_insn_detail_armv

为了解决这个问题,您需要告诉 C++ 编译器在声明这些函数名称时期望它们的非损坏形式:

extern "C" {
void print_insn_detail_arm();
}

最后,值得将它放在 C++ 和 C 的头文件中,这样如果你改变接口,你就会得到很好的编译时错误。为此,您需要对 C 编译器隐藏 extern 位,因为它不理解:

#ifdef __cplusplus
extern "C" {
#endif
void print_insn_detail_arm();
#ifdef __cplusplus
}
#endif

有关更多背景信息,请参阅Combining C++ and C - how does #ifdef __cplusplus work?

【讨论】:

  • 非常感谢,我没有注意到有c++调用c函数的问题。这是一个重要的次要观点
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-02
相关资源
最近更新 更多