CMake issues

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

**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_minimum_required(VERSION 3.5)



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.

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

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().

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

void like_this();

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".

1 Like

#ifndef FOO_H
#define FOO_H
void foo();
#endif // FOO_H
#include <stdio.h>
#include “foo.h”
void foo()
#include “foo.h”
int main()
return 0;

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.

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?

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.

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).

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.

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.

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?

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).

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