cancel
Showing results for 
Search instead for 
Did you mean: 

Using -include file GCC option causes errors.

BobaJFET
Associate III

I have an STM32H743 project generated and i'd like to include an external file using the GCC "-include file" command option. I've added it as shown in this screenshot under C/C++ Build > settings > MCU/MPU GCC Compiler > Miscellaneous

BobaJFET_0-1738262058529.png

The problem is the compiler doesn't seem to find file.c, no matter where I place it within the project directory. I've tried placing inside the same folder as main.c with no success. 

Here's a good reference to where this option comes from. 

Preprocessor Options (Using the GNU Compiler Collection (GCC))

The error:

<command-line>: fatal error: file.c: No such file or directory
compilation terminated.

 

Update: I think i've figured out how to get it to know where the file is, by adding the path under C/C++ General > Paths and Symbols > Source Location. But I still have these make errors: 

make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_adc.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_adc_ex.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_cortex.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_dma.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_dma_ex.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_exti.o] Error 1
make: *** [Drivers/STM32H7xx_HAL_Driver/Src/subdir.mk:88: Drivers/STM32H7xx_HAL_Driver/Src/stm32h7xx_hal_flash.o] Error 1

 

I've used this -include option in other IDE's before with no problems but i'm a bit new to cubeIDE.

 

 

10 REPLIES 10
TDK
Guru

Source files (*.c) shouldn't be included. They are not within the include path if you are using an inc/src folder structure.

 

Include folders should be listed here for the various compilers. I should have circled the "GCC Compiler" one instead of assembly.

TDK_0-1738262908421.png

 

Source folders should be listed here:

TDK_1-1738262942215.png

If you feel a post has answered your question, please click "Accept as Solution".
AScha.3
Chief III

So you want just add a file to a project ?

Then IDE should know this :

open your project (left pane) and right click on the project name : select : import.

then you see:

AScha3_0-1738262961782.png

then use > file system -> and set directory for import, then select .c or .h files and the folder, you want to have them.

Then IDE knows...if using include xx.h , what you want and will compile the c files imported also.

/* USER CODE BEGIN Includes */
#include <stdlib.h>
#include <string.h>
#include "math.h"
#include <stdio.h>
#include "ili9341.h"

here i include ... graphics driver for ili9341 .

If you feel a post has answered your question, please click "Accept as Solution".

What is the difference between a source folder and an include folder? 

I'm aware that files can be included into compilation with a #include.  

I want to tell the compiler, or the IDE to "add" the #include files automatically without having to keep #includes in the source code. This is what the -include file gcc option normally does. 

>>What is the difference between a source folder and an include folder? 

In the broader sense it is so stuff that's shared among multiple projects isn't duplicated into each. Say math, stdio and string libraries.

An Include Path provides an ordered list about where to search for them

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Source folders include source (*.c) files. The compiler will compile all source files found within the source folders.

Include folders include header files (*.h) and are found in #include statements. The path to a given header file must appear in the include directories in order for it to be found.

If you feel a post has answered your question, please click "Accept as Solution".
Pavel A.
Evangelist III

@BobaJFET Check your preprocessor include path. From the gcc docum: "the first directory searched for file is the preprocessor’s working directory instead of the directory containing the main source file. If not found there, it is searched for in the remainder of the #include "…" search chain as normal."

 


@BobaJFET wrote:

The problem is the compiler doesn't seem to find file.c,


That's because you've just given its name - the compiler also needs to know its location.

By using this option, you're treating file.c as an "header" (.h) file, rather than a normal source file - so have you tried putting it in the same folder as other header files which are found?

As others have said, #including .c files is generally considered poor practice - so why, exactly, do you want to do this?

Why not just add the .c file to the Project like all the other .c files.


@BobaJFET wrote:

What is the difference between a source folder and an include folder? 


There isn't any difference in the folders themselves - they are all just folders.

The difference is just in how you use them:

  • A source folder is a folder where you put source (usually .c) files;
  • An include folder is a folder where you put include (usually .h) files.

It's a common practice to have separate folders for source files & include files - but it's not essential.

You can have both source files & include files in the same folder, if you wish.

The important thing is that you tell the compiler where to look for the include files - by setting your Include Path:

https://gcc.gnu.org/onlinedocs/cpp/Search-Path.html