Skip to main content
MGuth.1
Associate III
March 10, 2023
Solved

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

  • March 10, 2023
  • 5 replies
  • 6924 views

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

This topic has been closed for replies.
Best answer by MGuth.1

Found the mistake:

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

5 replies

Pavel A.
Super User
March 10, 2023

#ifdef __cplusplus

MGuth.1
MGuth.1Author
Associate III
March 10, 2023

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 :grinning_face_with_sweat:​

Pavel A.
Super User
March 11, 2023

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

MGuth.1
MGuth.1Author
Associate III
March 11, 2023

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

Piranha
Principal III
March 11, 2023

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

MGuth.1
MGuth.1Author
Associate III
March 11, 2023

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

Karl Yamashita
Lead III
March 11, 2023

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

If a reply has proven helpful, click on Accept as Solution so that it'll show at top of the post.CAN Jammer an open source CAN bus hacking toolCANableV3 Open Source
MGuth.1
MGuth.1Author
Associate III
March 11, 2023

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)

MGuth.1
MGuth.1AuthorBest answer
Associate III
March 11, 2023

Found the mistake:

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