Is it possible to include C ++ libraries in C programs?

I work with a contractor who develops a library for us in C ++. I would like to know if this library can be used in a C program. I use Gcc as my compiler.

+7
c ++ c gcc
source share
4 answers

No, It is Immpossible. One thing to be skipped is the exception handling functions. You must compile the main file using the C ++ compiler.

If you really want to develop in c and use the C ++ library, you can develop a c-library and compile main with g ++.


Even if compilation succeeds, the link will not end at the end. See: Why can't I link a mixed C / C ++ static library to a C interface using gcc?
And this is not only the exception of functionality is missing. There are many other things that can be easily solved using g ++ to link everything.

As I said above, the solution is to call some function from main and associate it with g ++:

#include "my_c_main.h" int main(int argc, char* argv[]) { return run_my_c_main( argc, argv ); } 
0
source share

Yes it is possible. However, as BoBTFish says in the comment above, you (or the contractor) should develop a C interface for the C ++ library:

  • write a header file that compiles in both C and C ++ and declares some extern "C" functions. The interfaces of these functions must be valid in C, which in terms of C ++ means that they use only POD types (for example, without links) and do not throw exceptions. You can declare C ++ non-POD classes as incomplete types and use pointers for them, so usually every non-static member function is wrapped with a function that takes a pointer that becomes this as its first parameter.
  • implement functions in C ++ to call the C ++ library
  • compile the library and shell as C ++
  • compile your program as C (you can #include header wherever necessary)
  • bind all this together with g ++ so that C ++ components can reference libstdC ++.

I suppose you can argue that since the program is connected with g ++, it is by definition a C ++ program that uses the C library (which contains main ), and not a C program that uses the C ++ library. Personally, I would not argue with this, it is important that none of the existing C-code does not change.

Example:

lib.h

 #ifdef __cplusplus extern "C" #endif int foo(); 

lib.cpp

 #include "lib.h" #include <vector> #include <iostream> int foo() { try { std::vector<int> v; v.push_back(1); v.push_back(1); std::cout << "C++ seems to exist\n"; return v.size(); } catch (...) { return -1; } } 

main.c

 #include "lib.h" #include <stdio.h> int main() { printf("%d\n", foo()); } 

to build

 g++ lib.cpp -c -olib.o gcc main.c -c -omain.o g++ main.o lib.o -omain 

The following also works instead of the third line if you want to make an arbitrary distinction between using gcc for reference and using g++ :

 gcc main.o lib.o -llibstdc++ -omain 

However, I'm not sure that gcc -libstdc++ will work the same as g++ for all possible code that could be in lib.cpp . I just tested it for this example, and of course there is a lot of C ++ there that I haven't used.

+21
source share

This small dynamic library (g ++) and program (gcc) compile and link perfectly:

Library.h header

 #ifdef __cplusplus extern "C" { #endif typedef struct TagPerson {} Person; extern Person* person_create(const char* name); extern void person_destroy(Person* p); extern const char* person_name(Person* p); extern Person* person_exception(Person* p); #ifdef __cplusplus } #endif 

C ++ source library.cc compiled with g ++ -shared -fpic library.cc -o libLibrary.so

 #include <iostream> #include <string> #include <stdexcept> #include "library.h" namespace Library { class Person { public: Person(const std::string& name) : m_name(name) { std::cout << m_name << std::endl; } const std::string name() const { return m_name; } void exception() { throw std::runtime_error(""); } private: std::string m_name; }; } // namespace N extern "C" { #define PERSON(P) ((Library::Person*)(P)) extern Person* person_create(const char* name) { try { return (Person*)(new Library::Person(name)); } catch(...) { // Error Procession; } return 0; } extern void person_destroy(Person* p) { delete PERSON(p); } extern const char* person_name(Person* p) { return PERSON(p)->name().c_str(); } extern Person* person_exception(Person* p) { try { PERSON(p)->exception(); } catch(...) { std::cerr << "Exception" << std::endl; } return 0; } } // extern "C" 

Source C main.c compiled with gcc main.c -lLibrary -o Test

 #include <stdio.h> #include "library.h" int main() { Person* p = person_create("Jaco Pastorius"); printf("%s\n", person_name(p)); person_exception(p); person_destroy(p); return 0; } 
0
source share

What exactly is the problem? Do you already have a library in the “wrong” language, or is it that your contractor wants to create a library in C ++, but you need it to link to C?

In the first case, if you defined things correctly, you should make it your problem for the contractor, but if you haven’t done this or if this is not possible, you need to write an interface, as Steve Jessop suggests above.

In the latter case, this is really not a problem: the contractor will need to make sure that it provides the code in a form that can be called from C and is safe / secure / stable to be called in this way. The internal code that does this work can really be something at all if it returns it for you in the right way. If you plan to create the library yourself, you will need the necessary build scripts that run the compiler correctly. I assume that there is a potential maintenance problem if your own programmers cannot deal with C ++, but this is a problem related to recruitment / employment rather than the technical details of its operation.

-one
source share

All Articles