0

I have a function written in c++ and declare it using extern "C" as below. vectors are used inside the test().

test.hpp

#include <vector>
#ifdef __cplusplus
extern "C" {
#endif
void test();
#ifdef __cplusplus
}
#endif

And calling it in C code as below:

#include "test.hpp"
void main(){
   test();
}

c++ code compiles fine using g++, while compiling c with GCC, I'm getting No such file or directory error.

fatal error: vector: No such file or directory
   16 | #include <vector>
      |          ^~~~~~~~ 
harry
  • 970
  • 6
  • 25
  • 1
    The `` header file is a C++ specific header file. It shouldn't be included in a C file. In this case, where you don't even use `std::vector`, why include `` at all? – Some programmer dude Oct 19 '21 at 06:22
  • this is a trimmed down version of the actual code, vectors are used in test(). – harry Oct 19 '21 at 06:24
  • 1
    So include the `` header in the C++ source file instead of the C++ header file? Or perhaps better yet, split your `test.hpp` into two parts: One C++-specific part named `test.hpp` and one that can be used from both C and C++ named `test.h`? Then `test.hpp` could include `test.h`, if needed. – Some programmer dude Oct 19 '21 at 06:27
  • 2
    So vector should be included in the `test.cpp` file, not in the `test.h` file. – Robert Andrzejuk Oct 19 '21 at 06:28
  • Moving to source file did the trick, Thanks a lot :) – harry Oct 19 '21 at 06:40
  • What should I do in case vector is an argument? – harry Oct 19 '21 at 07:00
  • 2
    if the vector is an argument you can't call it from c so it doesn't belong in your c-specific header file – Alan Birtles Oct 19 '21 at 07:10

0 Answers0