これを行う方法を説明するダースページを読んでいます。どのようにextern "C"を使ってC++関数をCで呼び出すのですか
私はC++で書かれたライブラリ、およびGNUコンパイラでコンパイルされたCで書かれたコードのセットを持っている、と私はちょうどC.
からC++コードを呼び出すことができるように見えることはできません
printHeadOO.hpp
#ifdef __cplusplus
extern "C"
#endif
void printThis(void);
printSrcOO.cpp
0123:私はC++で書かれたライブラリを持っています私はこれをC++プログラムから呼び出すと、コンパイルされ、実行され、すべてがうまくいきます。しかし、私はCプログラムからそれを呼び出す代わりとします
printMain.c
#include <stdio.h>
#include "printHeadOO.hpp"
int main(void){
printf("This is printed from C Main \n");
printThis();
return 0;
}
ので、すべてがとうまくコンパイル:
#g++ -c printSrcOO.cpp
#gcc -c printMain.c
が、その後、リンク時:
#gcc printSrcOO.o printMain.o -o myProgram
gcc yaks以下:
printSrcOO.o: In function `__static_initialization_and_destruction_0(int, int)':
printSrcOO.cpp:(.text+0x23): undefined reference to `std::ios_base::Init::Init()'
printSrcOO.o: In function `__tcf_0':
printSrcOO.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::~Init()'
printSrcOO.o: In function `printThis':
printSrcOO.cpp:(.text+0x83): undefined reference to `std::cout'
printSrcOO.cpp:(.text+0x88): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
printSrcOO.o:(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
gccでgccが使用するフックを作成するにはどうすればよいですか?私はちょうど何か愚かな/何かを逃している?
TIA ダン
うわー...すごくいい答え。あなたの2番目の提案はまさに私が探していたものです!ありがとう – user1892929