cancel
Showing results for 
Search instead for 
Did you mean: 

C compiler

scott239955
Associate II
Posted on March 20, 2003 at 06:41

C compiler

9 REPLIES 9
scott239955
Associate II
Posted on May 17, 2011 at 11:33

This may be a general question regarding standards in C, but it may not. When using multiple files, I am use to doing:

(1) Declaring varibles, structures, and functions in one source file.

(2) Defining them as extern in a header file (if needed.)

(3) including that header file in the source files where needed.

I noticed that this compiler does not seem to care if extern is used so I have been just declaring them in the header file and not also in the one souce file, but I noticed something that did not work right. The attached code did not work unless the structure 'Programs' was defined in the same C source file (not the header file). Specifically, the line ''structure_address = (sizeof(Program)) * (program_num);'' generated an error ''undefined reference to `__mulsi3'

''

The compiler does not seem to allocate space more than once for included varibles in header files without the extern declaration, but should I go back to using 'extern' and to making definitions in both the header and source file? I guess that would be for functions as well? I never liked defining things twice in C.

**** added the follwoing later *****

I went back and looked at this again and discovered that the error was being caused by the size of the structure (~300 bytes) and not where it is included in the file. I still have the questions regarding 'extern' above in addition to why that line of code that multiples the size of the structure with an unsigned char gernates an error?

[ This message was edited by: Smiles on 06-03-2003 02:37 ]
romain2399
Associate II
Posted on May 17, 2011 at 11:33

Concerning the general C question, you should:

1/ Declare variables without using the extern statement, and write the functions in the foo.c source file. Declare variables as static if you want to protect them from external access.

2/ Create a foo.h header file with function prototypes and variables as extern if you want to allow their access from other files.

3/ Include the foo.h header in all the source files that use either the functions or the variables.

Notes:

- it is possible to include the foo.h header in the foo.c source file. This is useful to provide the function prototypes to all the functions. Some variables will be first declared as extern then declared inside the source file, this does not create any trouble.

- If you do not use the extern statement in the header file, each source file with a foo.h included has its own variable, this is no more a global variable.

I am not sure to understand if you have another problem than this C coding rule.

Romain
scott239955
Associate II
Posted on May 17, 2011 at 11:33

Thank you for the information. Yes, I still have a specific problem with the compiler generating an error - seems like it is looking for a library or something. I get the following error:

''undefined reference to `__mulsi3'

becasue of this line in the attached code:

''structure_address = (sizeof(Program)) * (program_num);''

Program is a structure of about 300 bytes and program_num is a varible. If I replace the varible with a constant like 450 , it compiles. Or, if I reduce the size of the structure it compiles. I also seperated this line into two lines to see where it was breaking down. The sizeof(Program) was ok, but not the multiplication by program_num. Sorry for the confusion - I orginaly thought this had something to do with the extern declaration issue, but it does not.

Posted on May 17, 2011 at 11:33

Can you send your complete project ?

Your compilation would be easier much easier to reproduce.

Jojo

scott239955
Associate II
Posted on May 17, 2011 at 11:33

Ok, I removed the extra code and left only the structure and function in question. The attached project generates the error. If you remark out some of the structure components (like the first two lines), it will compile without error. It will also compile without error if you replace 'struct_num' with a constant.

albert23
Associate II
Posted on May 17, 2011 at 11:33

You have to add the run-time library to your ''LIBS ='' line in your make file. Please add:

# List of libraries

LIBS = C:\ST9PlusV6.1.7\lib\gcc-lib\st9-elf\2.7.2\mmedium\libgcc.a

and it will fix your problem. Also, please read the documentation regarding Libraries in the ''User Documentation'' folder, which is installed with the STVD9 software.
romain2399
Associate II
Posted on May 17, 2011 at 11:33

You should not include the ''mmedium\libgcc.a'' library this way.

You need to specify to the linker that all the standard include libraries must have been compiled with the memory model you have chosen.

This is done with the following linker call:

LDFLAGS = -nostartfiles -T$(SCRIPTFILE) -Wl,''-Map $(APPLI).map'' -m$(MODEL)

There are several sets of standard libraries depending on your programming choices: user stack, floating point, memory model ....

scott239955
Associate II
Posted on May 17, 2011 at 11:33

Ok thanks, I will use the -nostartfiles linker option. I did try to just include the library and that also worked. Attached is the two pages from the manul that explains the -nostartfiles and -nolibs option. ST might want to reflect what you mentioned there.

I have to say that gcc is new to me. The original ST92F150 sample code I used to get started used the -nostdlib option. Is there any reason to use the -nostdlib option instead of the -nostartfiles option?

romain2399
Associate II
Posted on May 17, 2011 at 11:33

With the -nostdlib option neither the standard libraries nor the standard start-up files are used, instead your own start-up files are used (crtbegin.spp ...).

With the -nostartfiles option, the standard libraries are used and not the standard start-up files.

If you do not need the standard lib (mainly the mathematics functions), use the -nostartfiles option.