Saqib Shamsi
Saqib Shamsi

Reputation: 542

Call C++ functions containing STL data types from C

I have a function in C++ that uses data types like vector and map from STL. Here is some sample code:

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 created a header file as follows:

mylib.h

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

The C++ file was compiled into the obejct code using the command

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

Then, I wrote a C program in order to use the function summation from it.

main.c

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

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

Now, when I compile the C file above using gcc,

gcc main.c mylib.o

I get the following error

/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 is imperative that I use gcc to compile the C file. Is there a way to make it work?

I tried looking for solution and stumbled upon the links below,

How to call C++ function from C?

Elegantly call C++ from C

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

but couldn't find a solution to my problem.

Upvotes: 4

Views: 539

Answers (2)

Chris Dodd
Chris Dodd

Reputation: 126263

Compile the C file by itself with the C compiler:

gcc -c cfile.c

then link all the files with the C++ compiler:

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

Note that you must compile the file with the main function with the C++ compiler. Any other files can be compiled with either, though you need extern "C" declarations to be able to call C code from C++ or create C++ functions that can be called from C

Upvotes: 2

Jitendra
Jitendra

Reputation: 11

I have build all the file in g++, am not getting linking error. can you build all the source file to gcc or g++.

Upvotes: 0

Related Questions