Can I Mix C and C++ Code in the Same Project?
Last Updated :
23 Jul, 2025
C and C++ both are very popular programming languages. There can be many situations when you might have to export some functionalities that are written in C programming language into your C++ code. Now the biggest question arises while making any project in C and C++ is that- "Can we Mix C and C++ Code in the Same Project? The answer is Yes, and in this article, we will learn how we can mix C and C++ code in the same project.
Mixing C and C++ Code in the Same Project
To mix C and C++ code in the same project we can use the extern keyword. The extern keyword is a special keyword in C and C++ that extends the visibility of the variables and the function declared in a source file to another source file. We can use this extern keyword to export the variables and functions from our C file and then we can use them in the cpp file. Following is the syntax to use the extern keyword for importing function in C++ source file from a C source file.
Syntax for extern
extern "C" {
Function_Name()
variable
}
where:
- Function_Name(): is the name of the function you want to import from the C file.
- variable: is the name of the variable you want to import from the C file.
To mix C and C++ together we can't simply compiler the C++ code. We must follow the below steps to mix the c and C++ code:
Steps to Compile C and C++ code in the same project
Let us consider the name of the c file is functions.c and the name of the cpp files is main.cpp:
1. Compile functions.c into an object file using the below command:
gcc -c functions.c -o functions.o
2. Compile main.cpp and link it with the object file
g++ main.cpp functions.o -o my_program
3. Run the executable file to get the output using the below command
./my_program
Note: Your C and C++ file must be present in the same environment.
C++ Program to Mix C and C++ Code in the Same Project
Let us define the C file first from where we will export the variables and functionalities:
C
#include <stdio.h>
void myFunction() {
printf("Hello Geeks");
}
Now in the Cpp file we will use the functionalities defined in the C file:
C++
#include <iostream>
using namespace std;
// Declaration of the C function
extern "C" {
void myFunction();
}
int main() {
// Calling the C function from C++
myFunction();
return 0;
}
Output
Hello Geeks
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
How to Create a Static Library in C++? C++ allows users to add their own libraries to reduce the amount of code we have to write in our program. A static library in C++ is a library that is linked to the program at compile-time. In this article, we will learn how to create a static library in C++ from scratch. Create Static Library in C+
3 min read
Competitive Coding Setup for C++ and Python in VS Code using Python Script Most of us struggle with using heavy software to run C++ and python code and things become more complicated when we have too many files in a folder. In this blog, we are going to create a python script with VS-code-editor that works out to do all your works. It creates the folder + numbers of files
3 min read
How to Compile C++ Code in macOS ? Compiling a C++ code means translating the program from the human-readable form (high-level language) to something a machine can âunderstandâ (machine language) so that the program can run on a machine. In this article, we will learn how to compile C++ code in macOS. C++ Program Compilation in macOS
2 min read
How Do I Create a Library in C++? Libraries are reusable code packages that can be imported into our program to use the code defined in them. In C++, libraries can be either static or dynamic. A static library is a library that is linked to the program at compile-time whereas dynamic libraries in C++ are linked at runtime but they h
4 min read
Creating a C++ reusable Header File and its Implementation Files Reusability is one of the most important concepts of Software Engineering. Reusability means developing code that can be reused either in the same program or in different programs. C++ allows reusability through inheritance, containership, polymorphism, and genericity. But, there is another way to d
4 min read
How to Compile a C++ Program Using GCC In C++, the GNU Compiler Collection (GCC) is one of the most popular C/C++ compiler that is used to compile and execute the C and C++ program. In this article, we will learn how to compile a C++ program using GCC. Compiling a C++ Program Using GCC The GNU Compiler Collection (GCC) is a versatile too
2 min read