Abhinav jayaswal
Abhinav jayaswal

Reputation: 31

Including a .c file in a c++ program

I am working on a project where half my code is in c and half is in c++.

Now I want to include the cprogram.c in my cppprogram.cpp.

When including a c file inside a c program you can use

#include "cprogram.c"

Anyways to do that in c++ for including a c program.

Upvotes: 0

Views: 471

Answers (2)

As long as you are using Make or Cmake, you can just add it with #include "cprogram.c". C++ is also compatiable with C so you can just add it.

Upvotes: 0

TheEagle
TheEagle

Reputation: 5992

Besides from that you normally don't include .c files, but .h files instead, and that the following is bad practice, you can include C files into .cpp / .hpp files by wrapping the include into an extern "C":

cppprogram.cpp

extern "C" {
#include "cprogram.c"
}

See here for some examples.

Upvotes: 2

Related Questions