Can I Mix C and C++ Code in the Same Project? Last Updated : 14 May, 2024 Comments Improve Suggest changes Like Article Like Report 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 ProjectTo 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 externextern "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 projectLet 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.o2. Compile main.cpp and link it with the object file g++ main.cpp functions.o -o my_program3. Run the executable file to get the output using the below command ./my_programNote: Your C and C++ file must be present in the same environment. C++ Program to Mix C and C++ Code in the Same ProjectLet 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 GeeksTime Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Can I Mix C and C++ Code in the Same Project? S satwiksuman Follow Improve Article Tags : C++ Programs C++ Extern "C" C-Misc Misc C++ CPP Examples +2 More Practice Tags : CPP Similar Reads Comparing Python with C and C++ In the following article, we will compare the 3 most used coding languages from a beginner's perspective. It will help you to learn basics of all the 3 languages together while saving your time and will also help you to inter shift from one language you know to the other which you don't. Let's discu 7 min read MakeFile in C++ and its applications In C++, building a simple program is easy with the help of CLI of the compiler. But it becomes progressively difficult to maintain and build the project when its size increases. We may need to write multiple lines of commands just to simply compile it. This can be simplified by using Makefile for th 5 min read 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 Difference between #include in C/C++ and import in JAVA #include in C/C++: In the case of C language, #include is a standard or user-defined file in a program that tells the preprocessor to insert the internal contents of another file into the source code of the program. Syntax: #include<stdio.h> Program 1:Below is a C Program to demonstrate the us 2 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 call function within function in C or C++ When we begin programming in C/C++, we generally write one main() function and write all our logic inside this. This approach is fine for very small programs, but as the program size grows, this become unmanageable. So we use functions. We write code in the form of functions. The main function alway 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 Like