0

I'm working on a c++ project with clion,using capstone,and I dont know to much cmake syntax for sure there is my project tree enter image description here

there is a function (called a()) used in main.cpp, declared in cstool.h and implemented in cstool.cpp

and there are function print_insn_detail_arm(),print_insn_detail_arm64(),print_insn_detail_x86() used in a(),

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

but there error when building

I think that the libcstool_arch.a didnot be linked to the project, but I dont know how to write correct sentence.

not sure if the header files are necessary for the three .c file

I think maybe there are some errors or imperfect thinking in cmake sentence sequence and other aspects, wishing for guidance and advices as a beginner

mxw
  • 23
  • 3
  • Welcome to Stack Overflow. Here we dislike **images** which just contain a **code** or/and an **error message**. Instead we expect these things to be included into the question post as **text**. Please, read [ask] and update your question accordingly. – Tsyvarev Jul 12 '21 at 10:28
  • CMake looks OK to me. Since you use those functions without a header file, I assume you do some extern / forward declaration of these functions? Maybe the argument types don't match with what is expected? – Alexey S. Larionov Jul 12 '21 at 10:35
  • Hello! You can refer to [this](https://stackoverflow.com/questions/39598323/how-to-properly-link-libraries-with-cmake?rq=1) post for linking static library with CMake. – Harsh patel Jul 12 '21 at 10:46

1 Answers1

0

You did everything right on the CMake front, but you did not take C++ name mangling into account. Concretely, C++ and C disagree on the symbol name that corresponds to print_insn_detail_arm():

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

In order to fix this, you need to tell the C++ compiler to expect the non-mangled forms of these function names when you declare them:

extern "C" {
void print_insn_detail_arm();
}

Finally, it is worth putting this in a header file for both C++ and C so you get nice compile-time errors if you ever change the interface. For that, you need to hide the extern bit from the C compiler, as it does not understand that:

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

For more background information, see Combining C++ and C - how does #ifdef __cplusplus work?

Botje
  • 26,269
  • 3
  • 31
  • 41
  • thanks a lot, I didnot notice there is c++ call c function problem. That's an important minor point – mxw Aug 03 '21 at 13:04