This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Which C file is compiled during download and loading?

Hi,

I wonder what file gets loaded and compiled into Micro-controller on this IAR IDE?

I know it is not possible to compile and load two C programs at the same time. If the main will be, so what is the point to have all of them within workspace?

Can anyone explain this please?

Thank you.

  • Hi Henry,

    There is a difference between a compiled and linked program output and a C-code source file.  Obviously, you only load one program onto the microcontroller.  However, that output program can be made up of any number of C source files.  The application in main utilizes function calls and other information from the other supporting C files.  All of the source files are compiled, and then they go through a process called linking, which ties all of the needed components together into one application that gets loaded.

    Walter

  • Walter, please let me pose my question in other way. I am wondering the results that I can get from the LCD or LED on the board comes from compiling of all "C"s? Or special C? As I rebuild all the "h", "C", and "lst" or other files and then load and debug?

    Let's say if I would like to see the impact of EEprom "C" file on the functionality of Micro, what shall I do? 

    BTW, I deleted the "main.c" and the program did not work!

  • Henri Aghaei said:
    BTW, I deleted the "main.c" and the program did not work!

    This clearly indicates that you shall learn C programming language before continue.

  • Henri Aghaei said:
    I am wondering the results that I can get from the LCD or LED on the board comes from compiling of all "C"s? Or special C?

    As Ilmars' answer implies, this is a rather generic question of C coding (or coding in general).

    However...

    Each .C file contains code. Usually, code that belongs to a certain aspect of an application, like EEPROM access, LCD output, Button checking, is put into its own .C file.
    The main application code then uses the code of these separate .C files (made known to it by the .h files which describe the contents/capabilities of their associated .c files).

    Once all .c file shave been compiler separately, the linker collects all parts of them which are used by another .c file (starting from main() ) and links all together into the final application, which is then uploaded to the MSP.

    Henri Aghaei said:
    Let's say if I would like to see the impact of EEprom "C" file on the functionality of Micro, what shall I do? 

    Look at the code in the 'eeprom.c' file and try to understand it :)

    Henri Aghaei said:
    BTW, I deleted the "main.c" and the program did not work!

    Obviously. Main is the staring point for the linker. If there is no main, nobody is using the other parts of the code and the linker won't link anything.

    The IDE documentation (linker/compiler uses guide) might contain some more details. But this is rather generic coding knowledge, mostly unchanged for some 30 years.

  • It has nothing to do with programming. It is to understanding the IDE functionality!

  • Henri Aghaei said:
    It has nothing to do with programming. It is to understanding the IDE functionality!

    It is the basic compiler/linker architecture which is independent of any IDE. It has been this way in ancient times and still is the same.

    An IDE sometimes obfuscates it by making the compiler/linker calls automatic.

    MSPGCC doesn't use any IDE at all, and also GCC. However, the way it works on them is the same. So neither the question nor the answer have anything to do with IDE functionality.

  • AusEng said:

    It has nothing to do with programming. It is to understanding the IDE functionality!

    I understand what you are asking, but I'm not the best one to answer your question. On some IDEs, to not compile and link one of the 'C' programs just right click on it and you will see "Remove From Project". Click that. Sometimes you will also see "Remove From Project" under the 'Project' tab. Otherwise just move the file you don't want to another location and see what happens.

  • Joseph Raslavsky said:
    Otherwise just move the file you don't want to another location and see what happens.

    If you move the file or remove it from the project, all you'll see is a linking error.
    As I said, the IDE may obfuscate (or handle) the management of project parts, but you still need to know why there are multiple .c files, what they do and what role they play in the whole project.

    Having a navigation system in your car doesn't enable you to drive. You still have to know the traffic rules (and you'll still need a license). And you still have to think yourself. (there have been cases where people have driven the car into a river - because they didn't notice that this section of the road was a ferry)

  • Jens-Michael Gross said:
    Having a navigation system in your car doesn't enable you to drive. You still have to know the traffic rules (and you'll still need a license). 

     

    Nice example! :)

  • BTW, I found thousands of files in my PC. Which ones are actually being used for what purpose?

  • old_cow_yellow said:

    BTW, I found thousands of files in my PC. Which ones are actually being used for what purpose?

    I understand what you mean but I was looking for a way to find out how a multiple C project works! Still have some confusion regarding the function which are used in the display part as I mentioned in other post of mine here! 

  • CaEngineer said:
    I understand what you mean but I was looking for a way to find out how a multiple C project works!

    The compiler compiles one C source file at a time, each by itself, into an object file. The C source file may include header files to know which functions or global variables or constants are available by other C files or libraries in the project.

    YOu add all C files, as well as all libraries you want to use to your project. When you say 'build', the compiler will compile all C files in the project, then run the linker.

    The linker will take all object files looking for one that contains the 'main' function. Then it looks which functions or globals are uses by main or other functions in the same object file, and links them to the other object files (recursively, so the object file with main may use a function in object file two which uses on in object file three etc.)  When all object files are linked, some function calls or global symbols may still not be resolved. Then the linker will go through the libraries you specified (in exact the order you specified them), and if they are found there, they are linked too (again recursively). Finally ending up with the standard library, which is always used even if you didn't specify it.

    So if you use a function X that is also defined in one of your C files, then this one is used, even if one of the libraries also contains a function X.

    Hopefully, all functions and globals used in your C code are found in one of your other C files, or in a library, and the linker will finally generate a binary file from all accumulated code, which will then be loaded into the MSP.

  • Thanks for your great explanation dear Jens-Michael.

    Jens-Michael Gross said:
    all functions and globals used in your C code are found in one of your other C files,

    Exactly. Because when I define a function more than one place, IAR announces me and give me an error or warning! 

  • Jens-Michael Gross said:

    The linker will take all object files looking for one that contains the 'main' function. Then it looks which functions or globals are uses by main or other functions in the same object file, and links them to the other object files (recursively, ...

    ...and the linker will finally generate a binary file from all accumulated code, which will then be loaded into the MSP.

    JMG:

      So if I understand you correctly, the linker pulls out only the needed variables and functions from the various object files and libraries (ignoring unneeded variables and functions) to make 'main' the executable? Well that would make the most sense, but I really don't know.

      

  • Joseph Raslavsky said:
      So if I understand you correctly, the linker pulls out only the needed variables and functions from the various object files and libraries (ignoring unneeded variables and functions) to make 'main' the executable?

    It depends on the compiler. Some compilers pack code in a way that multiple functions share the same exit code or use a virtual function for common code sequences. In this case, the compiler declares the whole c file as a monolithic block, so the linker can only take all or nothing. On MSPGCC, there is a separate switch to make the compiler generate separate independent blocks from functions, so the linker can pull them independently.

    Libraries are usually built the way that each function was in its own c file and compiled separately (like a ZIP archive of many independent files), but with just one header file for all. Here the linker can pull what it needs and leave the rest.

  • A question!

       I have a variable in the "Background.C" that I need it to be initialized in the "Main.C". But when I initialize it in the "Main", I get the error that says it is "undefined"!!!

    So, it seems it is not a global variable. But the problem is that I have no clue where it is defined! 

    I use IAR, when I right click on the variable and go to the option which is called "Go to definition" or look for it in the "Find" there is no sign of it!!!

    How come?! In the background I can change it and play with but nowhere else!!!

    Any idea?

  • Is the error from compiler or linker?

    If compiler: when compiling main.c, the compiler doesn’t know what’s in background.c
    That’s where header files come into play: they tell the compiler ‘hey, somewhere out there is a function with this name and these parameters. And a global variable with that name and type’.
    So the compiler can generate the code around it and leaves it to the linker to fill-in the final memory address of this variables (hence the name ‘linker’ as it resolves the links between the independent object files from separate .c files compilations.)

    If a variable isn’t used anywhere, the linker can discard it. Like a function that is never called in any of the object files that are linked.

    If the error is form the linker, then you only declare the variable (as extern) but never define it. So the compielr assume sit is there, but the linker cannot find it later.

**Attention** This is a public forum