GCC module dependencies - c

Is there a way to compile app which using cross module dependencies?
When I try to compile modules using standard function & other module functions
gcc module.c -c
gcc module2.c -c
gcc module.o module2.o -o app
I get errors like
implicit declaration of function printf
I know it can be handled by including all headers in each file and using #define & #ifndef but it's very ugly. I'd like to include all files in app file like this:
app.c
#include "macro.h"
#include "module.h"
#include "module2.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
int main(void) {}
(module.h & module2.h omitted)
macro.h
#define macro(var1, var2) var1 ? printf(var2) : moduleFunc(var2)
#define macro2(var) some math func
module.c
void moduleFunc(char* var) {macro2(); module2Func();}
module2.c
void module2Func(...) {macro(); printf(...); some math func}

Include stdio.h in your macro.h. That way any module, that is trying to macro.h header will use printf declaration from stdio.h

Related

How can I link multiple c-files together using headers?

I work on a project that has multiple c files. Each c file has its own header. Now I want to put all c files together.
As a preperation I have tried the following thing:
This would be my example c-code (function.c):
#include <stdio.h>
#include "function.h"
void output()
{
printf("Thats a text\n");
}
Thats the associated header file (function.h):
//header function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
#endif // FUNCTION_H_
And thats my main.c:
#include "function.h"
int main()
{
output();
return 0;
}
I would expect the following output:
"Thats a text"
But I only receive following error:
undefined reference to 'output'
What am I doing wrong here?
Thanks a lot!
You need the prototype for output function in your header so that it's visible in other module(s).
//header function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
void output(void);
#endif // FUNCTION_H_
And you need to link the module (source file function.c) in order to actually provide the definition of output that your main module uses.
For example, you can directly compile them together with:
gcc main.c function.c -o my_out
You may also want to look at Makefiles as well.
Your header should be
//header function.h
#ifndef FUNCTION_H_
#define FUNCTION_H_
void output();
#endif // FUNCTION_H_
compile like this:
(actual flags may depend on compiler used)
cc -c main.c
(creates main.o)
cc -c function.c
(creates function.o)
cc main.o function.o
(creates a.out or whatever your system default is)
...or as someone else mentioned:
cc main.c function.c
(does it all)

gcc can't find define in header

I'm using a header called lib.h to organize my source code. The header is like:
#define TOT_REP 10
#define TOT_PAT 10
#define TIME_REP 15
The source file include the header, but when i compile with gcc i'm getting this:
error: ‘TIME_REP’ undeclared (first use in this function)
So i tried to compile with gcc -E -dM and i got something like this:
...
#define SIGUSR2 12
#define TIME_REP 15
#define ____mbstate_t_defined 1
#define __SIGRTMIN 32
...
I also tried with gcc -E and in the outuput i found that the macro is properly replaced with its value.
What can I do to solve this?
EDIT: The code where TIME_REP is used is this:
while((!ending|| *(shmAddress+0)!=0)&& quitSignal==0){
totFolder=0;
buf=(char*)calloc(2,sizeof(char));
patientString=(char*)calloc(2,sizeof(char));
sleep(TIME_REP);
while(read(fd,buf,sizeof(char))>0){
/*read from a file and get some data*/
}
}
EDIT 2: I tried to rename the lib.h and it seems to work now but i just can't understand why if with gcc -E -Dm found the macro then i can't compile the code.
Anyway to answer to Woodrow Barlow:
i have the lib.h and a rep.c the rep.c include the lib.h and other headers:
#include <stdio.h>
#include <stdlib.h>
#include <sys/shm.h>
#include <unistd.h>
#include <string.h>
#include "ReaderWriter.h"
#include "lib.h"
To compile I use gcc rep.c -o rep -Wall -pedantic

Compiling C with non standard header

I have my main C file:
#if defined(WIN32)
#include <windows.h>
#include <stddef.h>
#endif
#if defined(LINUX)
#include <curses.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(WIN32)
#include <conio.h>
#endif
#include <ctype.h>
#include <a429usbnt.h>
#if defined(WIN32)
#include "genlib.h"
#endif
void main()
{
_open_xpc(1);
}
When I try to compile using this command
gcc -I. -L. test.c -o test
I get the following error: undefined reference to '_open_xpc'.
However if I change the call to the _open_xpc function and instead just
printf("%d", XPC_ERROR_ACTIONCODE);
the program compiles fine and the correct value assigned to the definition of XPC_ERROR_ACTIONCODE is printed out, so the compiler is linking a429usbnt.h but will only recognize defined variables and not the functions.
If you are trying to link against a .lib file with gcc, it seems you need to define a directory with -L and an actual file with -l

C preprocessor directive error

I have a problem when i want use his scripts:
lib1.h
...
#ifdef LIB1_01
int lib1func(void);
#endif
...
lib1.c
...
#ifdef LIB1_01
int lib1func(void){
...
}
#endif
...
main.c
#define LIB1_01
#include <lib1.h>
int main(){
...
int x = lib1func(void);
...
...
I want use lib1func() when #define LIB1_01 is declared but I have an 'warning : implicit declaration of function' error when i use it...why ? Can you help me ?
Best regards.
Recommended alternative:
lib1.h
#ifndef LIB1_H
#define LIB1_H
int lib1func(void);
#endif
...
lib1.c
#include "lib1.h"
int lib1func(void){
...
}
main.c
#include "lib1.h"
int main(){
...
int x = lib1func(void);
...
...
NOTE:
1) You should declare "int lib1func(void)" in the header, but you may define it anywhere. In lib1.c (if you prefer), or even main.c. Just make sure you only define it once.
2) Note the use of the guard around the entire header body.
3) Also note the use of include "myheader.h" (for your own header files), vs. #include <systemheader.h>. The "<>" syntax should be used only for system headers.
To use that kind of includes, compile with option I.
gcc myfile.c -o myfile -I .
The . symbol means look in the current directory.

How to have the compiler put a header file in a source file

I know you can put a header file on top of a file by using the -include compiler flag in gcc, but is it possible to include the header file at the end of other header file declarations of a file. So for example, I have the following declarations in a C source file.
#include "a.h"
#include "b.h"
I would like it, to become
#include "a.h"
#include "b.h"
#include "inserted.h"
rather than
#include "inserted.h"
#include "a.h"
#include "b.h"
Use
-include a.h -include b.h -include inserted.h
Add header protection to all *.h files (which should be there anyway ... ;-).
You can't do exactly what you are asking. There is no way to tell the compiler to insert a header file at a random point in a file. But maybe you can get something close.
First make all the declarations in inserted.h as a macro:
#define DECLARE_INSERTED_H \
int gFoo = 0; \
void functionBar(); \
Then in your c file:
#include a.h
#include b.h
#ifndef DECLARE_INSERTED_H
#define DECLARE_INSERTED_H
#endif // !DECLARE_INSERTED_H
DECLARE_INSERTED_H
Then compile with -i inserted.h

Resources