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);
Related
Can anyone explain how to create a header file in C with a simple example from beginning to end.
foo.h
#ifndef FOO_H_ /* Include guard */
#define FOO_H_
int foo(int x); /* An example function declaration */
#endif // FOO_H_
foo.c
#include "foo.h" /* Include the header (not strictly necessary here) */
int foo(int x) /* Function definition */
{
return x + 5;
}
main.c
#include <stdio.h>
#include "foo.h" /* Include the header here, to obtain the function declaration */
int main(void)
{
int y = foo(3); /* Use the function here */
printf("%d\n", y);
return 0;
}
To compile using GCC
gcc -o my_app main.c foo.c
#ifndef MY_HEADER_H
# define MY_HEADER_H
//put your function headers here
#endif
MY_HEADER_H serves as a double-inclusion guard.
For the function declaration, you only need to define the signature, that is, without parameter names, like this:
int foo(char*);
If you really want to, you can also include the parameter's identifier, but it's not necessary because the identifier would only be used in a function's body (implementation), which in case of a header (parameter signature), it's missing.
This declares the function foo which accepts a char* and returns an int.
In your source file, you would have:
#include "my_header.h"
int foo(char* name) {
//do stuff
return 0;
}
myfile.h
#ifndef _myfile_h
#define _myfile_h
void function();
#endif
myfile.c
#include "myfile.h"
void function() {
}
header files contain prototypes for functions you define in a .c or .cpp/.cxx file (depending if you're using c or c++). You want to place #ifndef/#defines around your .h code so that if you include the same .h twice in different parts of your programs, the prototypes are only included once.
client.h
#ifndef CLIENT_H
#define CLIENT_H
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize);
#endif /** CLIENT_H */
Then you'd implement the .h in a .c file like so:
client.c
#include "client.h"
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize) {
short ret = -1;
//some implementation here
return ret;
}
I have defined an enum in a header file,
global.h:
Typedef enum
{
ELEMENT1,
ELEMENT2,
ELEMENT3
}e_element;
I have a second file using the enum as a function parameter.
file2.c
#include global.h
#include file2.h
Function(e_element x)
{
Body…
}
The prototype is in:
file2.h
Function(e_element x);
The compiler doesn’t know e_element in file2.h. I have tried putting the #include for global.h in both file2.c and file2.h, but it still doesn’t see it. I would put the enum in file2.h, except that it is used by several other files, so if I move it the problem will just show up somewhere else.
How can I get the file2.h prototype to see e_element?
This worked for me:
global.h
#ifndef GLOBAL_H
#define GLOBAL_H
typedef enum
{
ELEMENT1,
ELEMENT2,
ELEMENT3
}e_element;
#endif
file2.h
#ifndef FILE_2_H
#define FILE_2_H
#include "global.h"
int test(e_element);
#endif
file2.c
#include "file2.h"
int test(e_element x)
{
return x == ELEMENT1;
}
int main() {
return 0;
}
edit:
#ifndef is conventionally used as a "header guard". It prevents a header file from being included multiple times by the preprocessor, which prevents things from being defined multiple times. It works by checking if a unique symbol has been defined before. If it has not, then it immediately defines it and then continues with the header file until #endif. If the symbol was already defined then it skips the guarded code completely, preventing multiple definitions. An example of multiple definitions would be if the same header file was included in a source and a header that the source also includes.
See this link for more information.
I got some compiler/linker errors and i don't know what is the correct method to proceed. I'm in this situation:
a.h: in this file is defined a function declared as "inline", for example: inline void foo1();
b.h: in this file is defined a function declared as "inline" that calls foo1(): inline void foo2();
main.c: there are some functions calls of both foo1 and foo2().
Now, if i declare foo1 and foo2 in a.h and b.h as extern inline void i got the following error:
prj/src/b.o: In function foo1': (.text+0x0):
multiple definition offoo1'
prj/src/main.o:(.text+0x0): first defined here make: *
[kernel] Error 1
What is the way which allow to compile and link without errors/warning in the situation i described?
From http://gcc.gnu.org/onlinedocs/gcc/Inline.html:
When an inline function is not static, then the compiler must assume
that there may be calls from other source files; since a global symbol
can be defined only once in any program, the function must not be
defined in the other source files, so the calls therein cannot be
integrated. Therefore, a non-static inline function is always compiled
on its own in the usual fashion.
In other words, without static, it emits a symbol for your inline function. If you happen to define that function in a header and include it in more than one compilation unit, then you end up with multiple (redefined) symbols. If you want to include the definition in the header, you should make it static.
I tried it and didn't get any errors
a.h
extern inline void foo1()
{
return;
}
b.h
extern inline void foo2()
{
foo1();
return;
}
main.cpp
#include "a.h"
#include "b.h"
int main() {
foo1();
foo2();
return 0;
}
Put the inline definitions in your .h file and in the .c files force an external definition.
For example:
// File: a.h
inline void foo1(void) { /*...*/ }
// File main.c
#include "a.h"
extern inline void foo1(void);
int main(void)
{
/*...*/
}
You may consider using header guards to prevent redefinition. The implementation of the files is as follows. I tried compilation for the following files using CMake and it worked without any problem.
a.h
#ifndef A_H
#define A_H
inline
void foo1()
{
return;
}
#endif
b.h
#ifndef B_H
#define B_H
#include "a.h"
inline
void foo2()
{
foo1();
return;
}
#endif
main.cpp
#include "a.h"
#include "b.h"
int main() {
foo1();
foo2();
return 0;
}
For some reason, I'm getting multiple declarations of content within my header file even though I'm using header guards. My example code is below:
main.c:
#include "thing.h"
int main(){
printf("%d", increment());
return 0;
}
thing.c:
#include "thing.h"
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
int something = 0;
int increment();
#endif
When I attempt to compile this, GCC says that I have multiple definitions of the something variable. ifndef should make sure that this doesn't happen, so I'm confused why it is.
The include guards are functioning correctly and are not the source of the problem.
What happens is that every compilation unit that includes thing.h gets its own int something = 0, so the linker complains about multiple definitions.
Here is how you fix this:
thing.c:
#include "thing.h"
int something = 0;
int increment(){
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
extern int something;
int increment();
#endif
This way, only thing.c will have an instance of something, and main.c will refer to it.
You have one definition in each translation unit (one in main.c, and one in thing.c). The header guards stop the header from being included more than once in a single translation unit.
You need to declare something in the header file, and only define it in thing.c, just like the function:
thing.c:
#include "thing.h"
int something = 0;
int increment(void)
{
return something++;
}
thing.h:
#ifndef THING_H_
#define THING_H_
#include <stdio.h>
extern int something;
int increment(void);
#endif
The header guards will stop the file from being compiled more than once in the same compilation unit (file). You are including it in main.c and thing.c, so it will be compiled once in each, leading to the variable something being declared once in each unit, or twice in total.
try to avoid defining variables globally.
use functions like increment() to modify and read its value instead.
that way you can keep the variable static in the thing.c file, and you know for sure that only functions from that file will modify the value.
The variable something should be defined in a .c file, not
in a header file.
Only structures, macros and type declarations for variables and function prototypes
should be in header files. In your example, you can declare the type of something as extern int something in the header file. But the definition of the variable itself should be in a .c file.
With what you have done, the variable something will be defined
in each .c file that includes thing.h and you get a
"something defined multiple times" error message when GCC tries to link
everything together.
what ifndef is guarding is one .h included in a .c more than once. For instance
thing. h
#ifndef
#define
int something = 0;
#endif
thing2.h
#include "thing.h"
main.c
#include "thing.h"
#include "thing2.h"
int main()
{
printf("%d", something);
return 0;
}
if I leave ifndef out then GCC will complain
In file included from thing2.h:1:0,
from main.c:2:
thing.h:3:5: error: redefinition of ‘something’
thing.h:3:5: note: previous definition of ‘something’ was here
Can anyone explain how to create a header file in C with a simple example from beginning to end.
foo.h
#ifndef FOO_H_ /* Include guard */
#define FOO_H_
int foo(int x); /* An example function declaration */
#endif // FOO_H_
foo.c
#include "foo.h" /* Include the header (not strictly necessary here) */
int foo(int x) /* Function definition */
{
return x + 5;
}
main.c
#include <stdio.h>
#include "foo.h" /* Include the header here, to obtain the function declaration */
int main(void)
{
int y = foo(3); /* Use the function here */
printf("%d\n", y);
return 0;
}
To compile using GCC
gcc -o my_app main.c foo.c
#ifndef MY_HEADER_H
# define MY_HEADER_H
//put your function headers here
#endif
MY_HEADER_H serves as a double-inclusion guard.
For the function declaration, you only need to define the signature, that is, without parameter names, like this:
int foo(char*);
If you really want to, you can also include the parameter's identifier, but it's not necessary because the identifier would only be used in a function's body (implementation), which in case of a header (parameter signature), it's missing.
This declares the function foo which accepts a char* and returns an int.
In your source file, you would have:
#include "my_header.h"
int foo(char* name) {
//do stuff
return 0;
}
myfile.h
#ifndef _myfile_h
#define _myfile_h
void function();
#endif
myfile.c
#include "myfile.h"
void function() {
}
header files contain prototypes for functions you define in a .c or .cpp/.cxx file (depending if you're using c or c++). You want to place #ifndef/#defines around your .h code so that if you include the same .h twice in different parts of your programs, the prototypes are only included once.
client.h
#ifndef CLIENT_H
#define CLIENT_H
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize);
#endif /** CLIENT_H */
Then you'd implement the .h in a .c file like so:
client.c
#include "client.h"
short socketConnect(char *host,unsigned short port,char *sendbuf,char *recievebuf, long rbufsize) {
short ret = -1;
//some implementation here
return ret;
}