cancel
Showing results for 
Search instead for 
Did you mean: 

How to exclude a single file from being build with a specific compiler/language in STM32CubeIDE?

MGuth.1
Associate III

I'm having trouble compiling a TouchGFX project in STM32CubeIDE.

As I need to call a C++ function (touchgfx::OSWrappers::signalVSync()) from inside a .c file (app_azure_rtos.c) I created a C++-linker function as described in this thread.

Also as described in aboves thread I'm getting an error when compiling. The Error says

../Middlewares/ST/touchgfx/framework/include/touchgfx/hal/OSWrappers.hpp:23:1: error: unknown type name 'namespace'
   23 | namespace touchgfx

So it seems that the C-Compiler tries to compile a .hpp header file.

How can I tell the C-Compiler to exclude a single file from being build? I can exclude a whole directory . But this makes the whole directory (all files included) unavailable for all compilers/languages. I'm on STM32CubeIDE 1.12.0

1 ACCEPTED SOLUTION

Accepted Solutions
MGuth.1
Associate III

Found the mistake:

I was still including the OsWrappers.hpp inside a C file. This caused the C linkage fault.

View solution in original post

11 REPLIES 11
Pavel A.
Evangelist III

#ifdef __cplusplus

MGuth.1
Associate III

Thanks for your answer.

As shown in the linked thread I'm using the #ifdef __cplusplus flag in the cpp_link.cop and cpp_link.hpp. The error comes up then including OSWrappers.hpp. Two thoughts on that:

1. a .hpp/.cpp​ file should by default only be compiled by the C++ Compiler, right?

2. I don't really want to add #ifdef __cplusplus to the OSWrappers.hpp as it's generated by the TouchGFX Designer/Framework and I don't want to re-do this step everytime I'm changing the GUI (and therefore generating the Code again with the flag missing)

So there has to be a better solution to this in my opinion 😅

Piranha
Chief II

Then make a C++ file with a C callable function and call that signalVSync() method from it.

> a .hpp/.cpp​ file should by default only be compiled by the C++ Compiler, right?

A .cpp file yes. A .h or .hpp file may be included in both .c and .cpp files, in this case C++ specific parts of the file must be surrounded by #ifdef __cplusplus.

If whole content of a .h or hpp file is C++ specific, ensure that it is not included by any C files.

Also, check that c/c++ compiler is properly selected by the file extension (.c vs cpp) and not forced to all C or all C++.

Karl Yamashita
Lead II

Can't you right click on the file in the tree, >Resource Configuration>Exclude From Build?

I didn't know that a .hpp file may be included in C code (properly). However, I made sure that the corresponding files are compiled with the C++ Compiler (right click on the file -> Properties -> C7C++General - Language Mapping -> GNU C++)

To be 100% safe about the files:

my cpp_link.hpp:

#ifndef CPP_LINK_HPP
#define CPP_LINK_HPP
 
#ifdef __cplusplus
extern "C"{
#endif
 
	void cpp_link_signalVSync(void);
 
#ifdef __cplusplus
	}
#endif
 
#endif /* CPP_LINK_HPP */

my cpp_link.cpp:

#include <cpp_link.hpp>
#include <touchgfx/hal/OSWrappers.hpp>
 
#ifdef __cplusplus
	extern "C" {
#endif
 
	void cpp_link_signalVSync(void)
	{
		touchgfx::OSWrappers::signalVSync();
	}
 
#ifdef __cplusplus
	}
#endif

I also tried different plalces to include the OSWrappers.hpp: in the cpp_link.cpp and in the cpp_link.hpp both inside a #ifdef __cplusplus and outside (as stated above).

I also just tried to (clean and) build the project with cpp_link.cpp and cpp_link.hpp both completely commented out and excluded from the build path.

Same result. So the problem should be somewhere else but in the cpp_link files

If I understand you correct that's just what I did

I can do that (right click -> Properties -> C/C++ General - Paths and Symbols -> (check) Exclude resource from build). But this makes it unavailable for all languages/compilers afaik. So This specific file isn't compiled by the C++ nor the C compiler. But obviously I do need it so it has to be compiled somehow.

Also forcing CubeIDE to use a specific compiler (GNU C++) for the file (right click -> Properties -> C/C++ General - Language Mappings -> GNU C++) doesn't change nothing (after cleaning and building the project)