Can I use C ++ libraries in a C program?

I am writing a program in C, but I would like to use dynamic libraries like vector. Is it possible to use C ++ libraries in a C program?

+3
source share
5 answers

No std::vector, no. All that has been fixed is right.

In general, this is not attractive for using C ++ code, but it can be done. You have to wrap the classes in simple non-classical functions that your C code can call, since C does not execute classes. So that you can use these functions from C, you then wrap them with a declaration extern "C"to tell the C ++ compiler not to manipulate names.

++ , C. :

// cout.cpp - Compile this with a C++ compiler
#include <iostream>

extern "C" {
    void print_cout(const char *str) {
        std::cout << str << std::endl;
    }
}

/* print.c - Compile this with a C compiler */
void print_cout(const char *);

int main(void) {
    print_cout("hello world!");
    return 0;
}
+11

, C ++ .

+2

, ++. , C, ++.

+1

, ++. , gcc.

IDE ++, C ++, .

, ++ - C. , ++ C C.

+1

std::vector - . , ++.

, vector, vector .

, . ++, ++, , extern "C".

+1

Source: https://habr.com/ru/post/1721635/


All Articles