Advertisement
devincpp

extern__c_code_call_cpp_function

Apr 11th, 2025 (edited)
289
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.76 KB | None | 0 0
  1. /******************************************************/
  2. // 1.h
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6.  
  7.     int func(int a, int b);
  8.  
  9. #ifdef __cplusplus
  10. }
  11. #endif
  12.  
  13.  
  14. /******************************************************/
  15. // 1.cpp
  16. #include "1.h" // 必须加这个头文件,这样才能编译成C符号!!否则会链接失败
  17.  
  18. int func(int a, int b)
  19. {
  20.     return a * b;
  21. }
  22.  
  23.  
  24. /******************************************************/
  25. // main.c
  26. #include <stdio.h>
  27. #include "1.h"
  28.  
  29. int main()
  30. {
  31.     printf("result=%d\n", func(123, 10));
  32.  
  33.     return 0;
  34. }
  35.  
  36.  
  37. /******************************************************/
  38. // Makefile
  39. main:
  40.         g++ -c 1.cpp
  41.         gcc -c main.c
  42.         g++ 1.o main.o -o main
  43.  
  44. clean:
  45.         rm -rf *.o main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement