# Can I compile .c and .cpp files seperately in a single project?

**URL:** <https://discourse.cmake.org/t/can-i-compile-c-and-cpp-files-seperately-in-a-single-project/10484>\
**Category:** Code\
**Tags:** os:windows, gen:makefiles\
**Created:** [March 26, 2024, 8:49am UTC](https://discourse.cmake.org/t/can-i-compile-c-and-cpp-files-seperately-in-a-single-project/10484 "2024-03-26T08:49:44Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Giri](https://discourse.cmake.org/user_avatar/discourse.cmake.org/giri/32/4438_2.png) [@Giri](https://discourse.cmake.org/u/Giri)\
**Post date:** [March 26, 2024, 8:49am UTC](https://discourse.cmake.org/t/can-i-compile-c-and-cpp-files-seperately-in-a-single-project/10484/1 "2024-03-26T08:49:44Z")

</div>

Hi,

I am developing a C project. I have a library with .cpp files (No other option). But my project has most of .c files. Finally, I have to connect the different file types.

Whenever I build a project, I get compilation-level errors. My c project is not recognizing C++ keywords like class, virtual, new etc.

1. Are there any commands or arguments from CMake to use .cpp files in the C project?
2. Can I mention the C++ and c compilers to build respective files in a project?
3. Do you have any solutions for this kind of problematic situation from CMake’s side?

I appreciate any help you can provide.

---

<div class="post-metadata">

**Author:** ![jwm](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jwm/32/654_2.png) [@jwm](https://discourse.cmake.org/u/jwm)\
**Post date:** [March 26, 2024, 5:24pm UTC](https://discourse.cmake.org/t/can-i-compile-c-and-cpp-files-seperately-in-a-single-project/10484/2 "2024-03-26T17:24:26Z")

</div>

In your `project()` call, just add `CXX` to the `LANGUAGES` argument, like

```cmake
project( foo
  VERSION 1.2.3.4
  DESCRIPTION "My annoying C and C++ project"
  LANGUAGES C CXX
)

```

From there, CMake should automatically know that **.c** files are C code and **.cpp** files are C++ code.

---

<div class="post-metadata">

**Author:** ![Oodini](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/o/db5fbb/32.png) [@Oodini](https://discourse.cmake.org/u/Oodini)\
**Post date:** [March 28, 2024, 10:47am UTC](https://discourse.cmake.org/t/can-i-compile-c-and-cpp-files-seperately-in-a-single-project/10484/3 "2024-03-28T10:47:52Z")

</div>

According the documentation, C ad CXX are the default values.  
I guess the problem won’t be solved.

---

<div class="post-metadata">

**Author:** ![jwm](https://discourse.cmake.org/user_avatar/discourse.cmake.org/jwm/32/654_2.png) [@jwm](https://discourse.cmake.org/u/jwm)\
**Post date:** [March 28, 2024, 4:34pm UTC](https://discourse.cmake.org/t/can-i-compile-c-and-cpp-files-seperately-in-a-single-project/10484/4 "2024-03-28T16:34:12Z")

</div>

What does your **CMakeLists.txt** look like?

From the [project](https://cmake.org/cmake/help/latest/command/project.html) documentation:

> `LANGUAGES <language-name>...`
> 
> Optional. Can also be specified without `LANGUAGES` keyword per the first, short signature.

and

> By default `C` and `CXX` are enabled if no language options are given.

I had the opposite problem; a C++ project was not compiling a C file. In that case, I had `LANGUAGES CXX` (only). Adding `C` to the list fixed the problem.

Your issue doesn’t have to remain unsolved, but if there is some reason my solution won’t work, you’ll need to give us more information.
