2016-12-28 20 views
4

STLのvectormapのようなデータ型を使用するC++の関数があります。ここでいくつかのサンプルコードは、次のとおりCのSTLデータ型を含むC++関数を呼び出す

mylib.cpp

#include "mylib.h" 
#include<vector> 
using namespace std; 

    int summation(int n) { 
    vector<int> numbers; 
    int sum = 0; 
    for(int i = 1; i <=n; i++) 
     numbers.push_back(i); 
    for(int j = 0; j < numbers.size(); j++) 
     sum += numbers[j]; 

    return sum; 
    } 

次のようにIは、ヘッダファイルを作成した:

mylib.h

#ifdef _cplusplus 
extern "C" { 
#endif 
extern int summation(int n); 
#ifdef _cplusplus 
}; 
#endif 

C++ファイルましたコマンドを使用してobejctコードにコンパイルします。

g++ -o mylib.o -c mylib.cpp 

次に、関数summationを使用するためにCプログラムを作成しました。今

#include<stdio.h> 
#include "mylib.h" 

int main() { 
    int n; 
    scanf("%d", &n); 
    printf("%d", summation(n)); 
    return 0; 
} 

main.cの、私はgccの

gcc main.c mylib.o 

を使用して、上記Cファイルをコンパイルするとき、私は次のようなエラー

/tmp/ccAMN2ld.o: In function `main': 
main.c:(.text+0x33): undefined reference to `summation' 
mylib.o: In function `std::vector<int, std::allocator<int> >::_M_insert_aux(__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, int const&)': 
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x26e): undefined reference to `__cxa_begin_catch' 
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2d7): undefined reference to `__cxa_rethrow' 
mylib.cpp:(.text._ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi[_ZNSt6vectorIiSaIiEE13_M_insert_auxEN9__gnu_cxx17__normal_iteratorIPiS1_EERKi]+0x2df): undefined reference to `__cxa_end_catch' 
mylib.o: In function `std::vector<int, std::allocator<int> >::_M_check_len(unsigned long, char const*) const': 
mylib.cpp:(.text._ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc[_ZNKSt6vectorIiSaIiEE12_M_check_lenEmPKc]+0x5b): undefined reference to `std::__throw_length_error(char const*)' 
mylib.o: In function `__gnu_cxx::new_allocator<int>::deallocate(int*, unsigned long)': 
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim[_ZN9__gnu_cxx13new_allocatorIiE10deallocateEPim]+0x1c): undefined reference to `operator delete(void*)' 
mylib.o: In function `__gnu_cxx::new_allocator<int>::allocate(unsigned long, void const*)': 
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x2c): undefined reference to `std::__throw_bad_alloc()' 
mylib.cpp:(.text._ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv[_ZN9__gnu_cxx13new_allocatorIiE8allocateEmPKv]+0x3c): undefined reference to `operator new(unsigned long)' 
mylib.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0' 
collect2: error: ld returned 1 exit status 

Itを取得私はgcを使うことが不可欠ですc。Cファイルをコンパイルします。それを動作させる方法はありますか?

私は

How to call C++ function from C?

Elegantly call C++ from C

http://www.cplusplus.com/forum/general/8997/

が、私の問題への解決策を見つけることができませんでした、解決策を探してみましたし、以下のリンクにつまずきました。

+1

CコンパイラがC++コンポーネントをコンパイルする方法を知っているのはなぜですか? C++コンポーネントを完全にコンパイルしてバイナリをリンクする必要があるかもしれません。 –

+0

この質問はあなたがリンクした人の欺瞞のように見えます。回答は一般的な解決策を記述しており、具体的なコードでそれらを実装する必要があります。 – ForceBru

+4

'g ++'を使ってコードをリンクします( 'gcc'ではなく、プログラムはC++プログラムなのでC++プログラムです)。 'main()'を含んでいるファイルをコンパイルするには 'g ++'を使うのが普通です。なぜならC++の特殊なプロパティを持っているからです(Cとは違って)、Cコンパイラを使って 'main () 'を返します。 –

答えて

3

Cコンパイラで、それ自体でCファイルをコンパイルします。

gcc -c cfile.c 

をその後、C++コンパイラですべてのファイルをリンク:

g++ -o a.out main.o cfile.o mylib.o 

(注)でファイルをコンパイルする必要がありますmainはC++コンパイラで機能します。 C++からCコードを呼び出したり、Cから呼び出せるC++関数を作成するには、extern "C"宣言が必要ですが、他のファイルもどちらかでコンパイルすることができます。

0

g ++ですべてのファイルをビルドしましたが、リンクエラーが発生していません。すべてのソースファイルをgccまたはg ++にビルドできますか?

関連する問題