# CMake issues

**URL:** https://discourse.cmake.org/t/cmake-issues/6147
**Category:** Usage
**Tags:** os:linux
**Created:** [July 25, 2022, 12:37pm UTC](https://discourse.cmake.org/t/cmake-issues/6147 "2022-07-25T12:37:18Z")
**Posts on this page:** 15
**Page:** 1

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 25, 2022, 12:37pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/1 "2022-07-25T12:37:18Z")

</div>

I installed Qt Creator version 7.0.2 with cmake and namke option few months back. In the IDE kit I am able to find Cmake version 3.21.1 and it is working with single source file. However with multiple files it is working with only .cpp files and not considering .h files. It is reporting undefined reference to function declared in .h file and defined in .cpp file, resulting build failure. If I change the #include from foo.h to foo.cpp it is working in the main file.  
I tried to check the same from linux shell. To my surprise it reported ‘cmake not installed’. I installed ‘cmake’ version 3.16.3 from command prompt but even simple tutorial example CmakeLists.txt is failing with following message

```auto
**cmake --build .**
[33%] Linking CXX executable app
/usr/bin/ld: CMakeFiles/app.dir/main.c.o: in function `main':
main.c:(.text+0xe): undefined reference to `foo'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/app.dir/build.make:99: app] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/app.dir

```

**The CmakeLists.txt file is as below.**

```cmake
cmake_minimum_required(VERSION 3.5)

project(Test)

include_directories(${PROJECT_SOURCE_DIR})

add_executable(app main.c foo.cpp)

```

I am not able to figure out what is the problem. Request some help as soon as possible as I am seriously stuck with this. Regards Soumitra G.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 25, 2022, 1:31pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/2 "2022-07-25T13:31:57Z")

</div>

Can you share the contents of the `main.c`, `foo.cpp`, and `foo.h` files?

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 27, 2022, 4:11pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/3 "2022-07-27T16:11:59Z")

</div>

I am finding difficulty to send the files as it is. However the code is very simple. The foo.h contain declaration of foo(), the foo.cpp includes the foo.h and define foo() which is only printing ‘Hello’, main.c includes foo.h and calls foo() from main().

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 27, 2022, 4:24pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/4 "2022-07-27T16:24:02Z")

</div>

How they are declared is quite important here. If they’re short, just put them directly into the comment:

```nohighlight
void like_this();

```

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [July 28, 2022, 5:12am UTC](https://discourse.cmake.org/t/cmake-issues/6147/5 "2022-07-28T05:12:22Z")

</div>

Wild guess - `main.c` is getting compiled as C source, including `foo.h`, and that results with symbol `_foo` being referenced (C “mangling”).  
Then `foo.cpp` and `foo.h` are compiled with C++ compiler, which creates symbol `_Z3foov`.

You either need to force compilation of everything as C++, or use `extern "C"`.

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 28, 2022, 1:48pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/6 "2022-07-28T13:48:13Z")

</div>

-------------foo.h----------  
#ifndef FOO\_H  
#define FOO\_H  
void foo();  
#endif // FOO\_H  
-----------------------------foo.cpp—  
#include \<stdio.h\>  
#include “foo.h”  
void foo()  
{  
printf(“Hello\n”);  
}  
-----------------main.c-------  
#include “foo.h”  
int main()  
{  
foo();  
return 0;  
}

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 28, 2022, 1:58pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/7 "2022-07-28T13:58:07Z")

</div>

Thanks for your suggestion. I understand your point but usually gcc handles such .c and .cpp mix ups. The code is perfectly behaving in nmake and both cmake and nmake are using gcc compiler. I am trying to understand any issue with cmake which is behaving peculiar. Actually the code and CMalkeLists.txt details are from one cmake tutorial. In QT Creator even the project is not building properly with cmake if further subfolder level inclusion but only with .h files. If I change to .cpp files in the #include it is working perfectly.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 28, 2022, 2:30pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/8 "2022-07-28T14:30:20Z")

</div>

> [@skgoswami](#):
>
> The code is perfectly behaving in nmake and both cmake and nmake are using gcc compiler.

Are you saying that the `.cpp` is also using the `gcc` compiler? CMake will look at the extension and use `g++` for the `.cpp` file. Can you get the exact commands used in your case that is working?

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 28, 2022, 5:29pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/9 "2022-07-28T17:29:12Z")

</div>

I am not using command line building as normally done with gcc from linux shell with individual files, instead using make option as per CmakeLists.txt defintion and make command as required for building the executive. Initially I tried in Qt Creator IDE, where default complier is gcc and result was same. Moreover if I change the #include from foo.h to foo.cpp everything works. Anyway the project is building perfectly in the same environment i.e. with default compiler gcc installed as default compiler of Qt Creator just changing the make option to nmake. Will it be possible for any of you to try the same CmakeLists.txt file shared in my issue reporting at your end with the same set of source files ? The example was taken from a tutorial on cmake but on build environment Visual Studio. I was to try some FMU aplication develpment using cmake and after landing in trouble with that I felt for trying a simple example before exploring anything more complex.

---

<div class="post-metadata">

### Author: ![ben.boeckel](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/b/ea5d25/32.png) [@ben.boeckel](https://discourse.cmake.org/u/ben.boeckel)
#### Post date: [July 29, 2022, 1:44am UTC](https://discourse.cmake.org/t/cmake-issues/6147/10 "2022-07-29T01:44:17Z")

</div>

I think a full example would be useful to help instead of guessing at what you’re using. The files you have (should) work if you treat everything as either C++ or C code (but not a mix).

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 29, 2022, 2:06am UTC](https://discourse.cmake.org/t/cmake-issues/6147/11 "2022-07-29T02:06:58Z")

</div>

Ok I will try that and let you know the progress. Regarding sharing full project in Qt Creator is only possible if I can get an email for correspondence. This forum resources are not permitting beyond this.

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 29, 2022, 12:59pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/12 "2022-07-29T12:59:44Z")

</div>

Many thanks for the guidance and after changing the main.c to main.cpp it worked. It is great learning as in ‘nmake’ I did not find any problem. First time switching towards ‘cmake’. Pl. advise some sources where very detailed idea on ‘cmake’ is possible.

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [July 29, 2022, 1:03pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/13 "2022-07-29T13:03:07Z")

</div>

Many thanks your advise finally found the solution. Need more to learn on ‘cmake’ before I can work on it further. Any advice over possible sources?

---

<div class="post-metadata">

### Author: ![fenrir](https://discourse.cmake.org/user_avatar/discourse.cmake.org/fenrir/32/734_2.png) [@fenrir](https://discourse.cmake.org/u/fenrir)
#### Post date: [July 30, 2022, 2:28pm UTC](https://discourse.cmake.org/t/cmake-issues/6147/14 "2022-07-30T14:28:33Z")

</div>

I’ve learned at first by being forced to dive into a project, that used (sub-optimal) CMake for building. Then I mostly read documentation on googled how to do stuff. The only real resource I can recommend is Craig Scott’s “Professional CMake: A Practical Guide”.

Good understanding of / experience in the languages you use also helps a ton. This issue basically screamed to me what was going (mostly because I’ve done the same mistake way to many times before - with and without CMake involved).

---

<div class="post-metadata">

### Author: ![skgoswami](https://discourse.cmake.org/letter_avatar_proxy/v4/letter/s/7cd45c/32.png) [@skgoswami](https://discourse.cmake.org/u/skgoswami)
#### Post date: [August 1, 2022, 2:04am UTC](https://discourse.cmake.org/t/cmake-issues/6147/15 "2022-08-01T02:04:32Z")

</div>

Many thanks to you. It will go a long way with me.
