简体   繁体   中英

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM