cancel
Showing results for 
Search instead for 
Did you mean: 

How does build order of source files are decided by compiler?

FErma
Associate II

Hi all,

I am using STM32F042 series mcu, and IAR as compiler.

I have some #define statements in main.h which provides some setting configurations. Forexample we have different custom pcbs which have either active low or active high button. If I place #define active_high it compiles button.c source file accordingly.

However, I noticed that some of my source files are built before main.c is builded. This causes my #define statements are not recognised by these source files. The simple way to solve this problem can be including main.h in each source file, but I want to learn how compiler decides order of source files which will be builded. Does anyone have any idea?

Is it possible to select main.c to be builded first?

Thanks in advance.

5 REPLIES 5

Why do other files have any dependency on how/when main.c builds? They need to create their own dependency on main.h if they are reliant upon a #define in there.

Project build order in things like Keil and IAR is predominantly the order in which they were added, usually alpha or reverse alpha sort depending on the file manager selection.

These tools typically create dependency trees based on the files they pull in, or if the compiler command line is changed. Should be able to see they have a dependency on main.h

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

Don't rely on mastering specific tool. Put your config stuff in a header file and include it in all c files. #include is one way. The other way is to use conditional code using label added at compiler invocation which is less portable.

KnarfB
Principal III

Each .c file is a separate compilation unit which is compiled independently of other compilation units. So the compilation order does not matter.

But: each .c file must include all the .h file it needs for its own compilation. So you might want to include main.h in every .c file.

TDK
Guru

If you #include main.h, it doesn't matter when main.c is built. It's independent.

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

In standard C development you must provide all .h files needed to compile a .c file. then the order doesn't matter at all