Setting function at specific address - c

I have the next single C code and I'm trying to compile for arm using arm-gcc GNU and eclipse.
main.h
#ifndef
#define MAIN_H_
extern int rmain(int,int);
#endif
main.c
#include main.h
#include sum.h
int rmain(a1,a2){
int z=0x89;
return sum(a1+a2)+z;
sum.h
#ifndef
#define SUM_H_
extern int sum(int,int);
#endif
sum.c
#include sum.h
int sum(int a1,int a2)
return a1+a2;
My linker script file look like on this way:
mem.lds
ENTRY(rmain)
SECTIONS:{
.=0x808080
.text:{*(.text)}
.data:{*(.data)}
.bss:{*(.bss)}
}
This work OK, but my problem is that I need that function rmain will be set at 0x808080 address, so How i can do it this ?
I tryed with:
SECTIONS:{
.=0x808080
.start:{main.o (.text)}
.text:{*(EXCLUDE_FILE(main.o).text)}
.data:{*(.data)}
.bss:{*(.bss)}
}
But not luck linker say: multiple definition of rmain.

Related

How to create a C library with CodeBlocks?

How can I create a C library in CodeBlocks that can be define and used like a standard library with the #include command?
In fact I want to create a simple library that is Composed of several functions.
Basically, you need a .h file for the header definitions and a .c containing the source code.
An example:
/* command.h */
#ifndef COMMAND_H
#define COMMAND_H
int func(void);
#endif /* COMMAND_H */
/* command.c */
#include "command.h"
int func(void)
{
return 0;
}
/* main.c */
#include <stdio.h>
#include "command.h"
int main(void)
{
printf("%d\n", func());
return 0;
}
ifndef is used to prevent the file from being included more than once.
Compile it including both .c files in the command line:
gcc -o demo main.c command.c
Or in your case, follow this guide to compile multiple files in codeblocks.

typedefs and #defines are invisible for other files in XCode

First of all I am sorry for the title. I really did not know how I could describe my problem in a better way.
When using XCode, I have this problem that "typedefs" and "#defines" only seem to be visible for the file where they are written in.
Let's assume I have three files. main.c, Foo.h, Foo.c
main.c:
#include <stdio.h>
#include <stdlib.h>
typedef int simpleInteger;
#include "Foo.h"
int main(int argc, const char * argv[]) {
simpleInteger I = 22;
printf("%d\n", Foo(I));
return 0;
}
Foo.h:
#ifndef Foo_h
#define Foo_h
simpleInteger Foo(simpleInteger number);
#endif /* Foo_h */
Foo.c:
#include "Foo.h"
int Foo(simpleInteger number)
{
return number*2;
}
When I try to compile this, XCode throws the error "Unknown type name 'simpleInteger'" in Foo.h and Foo.c.
To make this to work I have to include the line "typedef int simpleInteger" in Foo.h which seems not clean to me. However if I compile these files without the use of XCode, it just works perfectly.
How can I tell XCode to not complain about this and make it work like any other compiler would do?
This needs to be in Foo.h, not main.c:
typedef int simpleInteger;

C preprocessor #error in header file included in multiple source files

I have two source files, main.c and datamgr.c - and two header files, config.h and datamgr.h
The testing system we're using expects these files, and only these files.
main.c:
#include "datamgr.h"
#include "config.h"
int main() {
custom_type a = 1;
a = foo();
return 0;
}
datamgr.c:
#include "datamgr.h"
#include "config.h"
custom_type foo() {
custom_type a = 1;
return a;
}
datamgr.h:
#ifndef DATAMGR_H
#define DATAMGR_H
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
custom_type foo();
#endif
config.h:
#ifndef CONFIG_H
#define CONFIG_H
#ifndef SET_MAX_TEMP
#error "Max temperature not set."
#endif
#ifndef SET_MIN_TEMP
#error "Max temperature not set."
#endif
typedef custom_type uint16_t
#endif
Now, the problem is that I can only define SET_MAX_TEMP and SET_MIN_TEMP in main.c, but both main.c and datamgr.c need both the header files. So if I leave them undefined in datamgr.c I get a compiler error. However, if I do define them in datamgr.c and later overwrite them in main.c, I get a different compiler error.
Please, any assistance as to how to get this horrible setup to work would be greatly appreciated.
You can pass these defines directly while compiling:
gcc -DSET_MAX_TEMP -DSET_MIN_TEMP <your files>
In datamgr.c do:
#define SET_MAX_TEMP
#define SET_MIN_TEMP
#include "datamgr.h"
#include "config.h"
#undef SET_MAX_TEMP
#undef SET_MIN_TEMP
In a comment, you said:
Because main.c is the file that our testing system uses to implement the test scenarios.
In that case, make sure that the testing system defines those macros in the command line of the compiler for every file being compiled.

Unresolved External Symbol with external get function

I keep getting a linker error with the following setup.
I have file1.c which contains the following code
#if defined( _TEST_ENABLED )
int get_value()
{
.
.
.
}
#endif /*_TEST_ENABLED */
I have file2.c which includes file2.h, which defines _TEST_ENABLED. file2.c makes a call to get_value(), however the linker isn't having any part of that.
I've exhausted a lot of different options with zero success. Now i'm asking for help :)
If file1.c does not include file2.h or any file which defines _TEST_ENABLED, _TEST_ENABLED will not be defined when the preprocessor runs on file1.c, so int get_value() { ... } will not get compiled.
In order to call a function in another file:
1) The files must be compiled or at least linked together. The easiest way to do this is gcc file1.c file2.c, however you can also compile both files to *.o files and then link together.
2) The calling file must have, usually through an included header, a prototype of the function. This prototype must appear before the function is used. So, if file2.h defines _TEST_ENABLED, then you must (in file2.c) include file2.h, and then either file2.c or file2.h must include file1.h, which must contain a function prototype (int get_value;)
For example:
file1.c
#include <file1.h>
#include <file2.h>
int main() {
get_value();
}
file1.h
#ifndef _FILE2_H
#define _FILE2_H
#define _TEST_ENABLED
#endif
file2.c
#include <file2.h>
#include <file1.h>
#ifdef _TEST_ENABLED
int get_value() {
return 42;
}
#endif
file2.h
#ifndef _FILE2_H
#define _FILE2_H
int get_value();
#endif
Note that for the purposes of the preprocessor, file1.c and file2.c are processed completely separately. When processing file2.c, it MUST find #define _TEST_ENABLED somewhere, which is why file2.c must include file1.h. Since this is getting a little circular, you should add "#include-guards to each header file, as shown above.
There are some ambiguities in your question, but given the following three files, I can compile and build in ANSI C, but I have to include the .h in both .cs:
file1.c
#include "file2.h"
int main(void)
{
someFunc();
get_value();
return 0;
}
#ifdef _TEST_ENABLED
int get_value(void)
{
return 0;
}
#endif
file2.c
#include "file2.h"
int someFunc(void);
int someFunc(void)
{
get_value();
return 0;
}
file2.h
#define _TEST_ENABLED
int get_value(void);

Experimenting with global variables and functions in C

I'm trying to understand how global variables and functions work in C. My program compiles and works fine with gcc, but does not compile with g++. I have the following files:
globals.h:
int i;
void fun();
globals.c:
#include "stdlib.h"
#include "stdio.h"
void fun()
{
printf("global function\n");
}
main.c:
#include "stdlib.h"
#include "stdio.h"
#include "globals.h"
void myfun();
int main()
{
i=1;
myfun();
return 0;
}
And finally, myfun.c:
#include "stdlib.h"
#include "stdio.h"
#include "globals.h"
void myfun()
{
fun();
}
I get the following error when compiling with g++:
/tmp/ccoZxBg9.o:(.bss+0x0): multiple definition of `i'
/tmp/ccz8cPTA.o:(.bss+0x0): first defined here
collect2: ld returned 1 exit status
Any ideas why? I would prefer to compile with g++.
Every file you include globals.h from will define "int i".
Instead, put "extern int i;" into the header file and then put the actual definition of "int i = 1;" in globals.c.
Putting header guards around globals.h would be sensible too.
Edit: In answer to your question its because a #include works kind of like a cut and paste. It pastes the contents of the included file into the c file that you are calling include from. As you include "globals.h" from main.c and myfun.c you define int i = 1 in both files. This value, being global, gets put into the table of linkable values. If you have the same variable name twice then the linker won't be able to tell which one it needs and you get the error you are seeing. Instead by adding extern on the front in the header file you are telling each file that "int i" is defined somewhere else. Obviously, you need to define it somewhere else (and ONLY in one place) so defining it in globals.c makes perfect sense.
Hope that helps :)
I would add an include guard in your globals file
#ifndef GLOBALS_H
#define GLOBALS_H
int i;
void fun();
#endif
Edit: Change your globals to be like this (using extern as the other answer describes)
globals.h
extern int i;
extern void fun();
globals.c
#include "stdlib.h"
#include "stdio.h"
int i;
void fun()
{
printf("global function\n");
}
I compiled it with
g++ globals.c main.c myfun.c
and it ran ok
Several things wrong here; several other things highly recommended:
globals.h:
#ifndef GLOBALS_H
#define GLOBALS_H
extern int my_global;
#ifdef __cplusplus
extern "C" {
#endif
void fun();
#ifdef __cplusplus
}
#endif
#endif
/* GLOBALS_H */
globals.c:
#include <stdlib.h>
#include <stdio.h>
#include "globals.h"
int my_global;
void fun()
{
printf("global function: %d\n", my_global);
}
main.c:
#include <stdlib.h>
#include <stdio.h>
#include "globals.h"
void myfun();
int main()
{
my_global=1;
myfun();
return 0;
}
void myfun()
{
fun();
}
You should declare "extern int myvar" in your header, and actually allocate "int myvar" in one and only one .c file.
You should include "globals.h" in every file that uses "myvar" - including the file where it's allocated.
Especially if you're planning on mixing C and C++ modules, you should use 'extern "C"' to distinguish non-C++ functions.
System headers should be "#include <some_header.h>"; your own headers should use quotes (#include "myheader.h") instead.
Short variable names like "i" might be OK for a strictly local variable (like a loop index), but you should always use longer, descriptive names whenever you can't avoid using a global variable.
I added a "printf" for my_global.
'Hope that helps!
I had this problem when porting some old C code to C++. The problem was it was a project that was connected to a database, and i wanted to port the database to c++ but not the rest. The database pulled in some C dependencies that couldn't be ported, so i needed the C code that overlapped both the database and the other project to compile in g++ as well as gcc...
The solution to this problem is to define all variables as extern in the .h file. then when you compile in either gcc or g++ it will report symbols missing in the .c files. So edit the .c files in the error messages and insert the declaration into all the .c files that need the variables. Note: you may have to declare it in multiple .c files, which is what threw me and why I was stuck on this problem for ages.
Anyway this solved my problem and the code compiles cleanly under both gcc and g++ now.

Resources