gcc can't find define in header - c

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

Related

Can't compile i2c_smbus_write_byte on Raspberry Pi 4

Has anybody tried using the i2c_smbus_write_byte or any similar function on Raspberry Pi 4?
I can't get it compile it fails at the linking with not finding it.
I'm using it as described here: http://synfare.com/599N105E/hwdocs/rpi/rpii2c.html
All the headers recommended are there is and also the -li2c in the Makefile.
Can anybody tell what the problem can be? I have no clue at the moment.
Might be worth checking to see if libi2c-dev is present on your system.
sudo apt-get install libi2c-dev
may be all that you need.
The page you are linking to says:
With the Buster version, as of june 2019, the necessary details for
using i2c_smbus_write_byte_data() and siblings, require the following
include statements:
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
Using fgrep you can confirm that the function is declared in the /usr/include/i2c/smbus.h:
# cd /usr/include; fgrep -R i2c_smbus_write_byte *
i2c/smbus.h:extern __s32 i2c_smbus_write_byte(int file, __u8 value);
i2c/smbus.h:extern __s32 i2c_smbus_write_byte_data(int file, __u8 command, __u8 value);
So this should work:
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
int main(void) {
int i2c = open("/dev/i2c-1", O_RDWR);
i2c_smbus_write_byte(i2c, 1);
close(i2c);
return 0;
}
I tested that this example compiles successfully in the latest Raspbian Buster Lite:
gcc test.c -otest -li2c
If you are using g++ instead of gcc, then you should wrap the include directives with extern "C":
extern "C" {
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
}

Scope and conditional #define in C

I want to use local .h file for define same names used in my lib. My lib has a default value definition for this names, but I'd like change this default value using local .h file. However, I'm having unwanted behaviour. How can I solve this?
test.c
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "conf.h"
#include "mylib.h"
int main ()
{
printf("Value in main: %d\n", NAMEDEFINITION);
fn();
return 0;
}
conf.h
#define NAMEDEFINITION 42
mylib.h
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#ifndef NAMEDEFINITION
#define NAMEDEFINITION 84
#endif
void fn();
mylib.c
#include "mylib.h"
void fn()
{
printf("Value in fn: %d\n", NAMEDEFINITION);
return;
}
My compiling line and output:
user#local:~/user/test/c$ gcc test.c mylib.c -o test
user#local:~/user/test/c$ ./test
Value in main: 42
Value in fn: 84
[EDITED]
I'd like NAMEDEFINITION be "42" when I define it in conf.h, then print "42" in two main() calls. When it is not defined in conf.h, it would be its default value, "84" (print "84" in two main() calls.
Value in main: 42
Reason for this is that in test.c you have included conf.h Now even though you include mylib.h, NAMEDEFINITION is visible in test.c and NAMEDEFINITION will have a value of 42. The ifdef in mylib.h will not be valid.
Value in fn: 84
In mylib.c you have not included conf.h. So the line #ifndef NAMEDEFINITION will be true and the value of NAMEDEFINITION will be 84.
If you want the value in fn to also print 42, you need to include conf.h in the mylib.c or mylib.h
Then if you comment out the line #define NAMEDEFINITION 42 in conf.h the value 84 will be printed both times.
I see 3 ways you could go with this:
1. Make config file mandatory part of your library
mylib.h
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include "conf.h"
#ifndef NAMEDEFINITION
#error NAMEDEFINITION hasn't beed defined! Please edit conf.h!
#endif
void fn();
2. Define NAMEDEFINITION with compilation option
Example:
-D NAMEDEFINITION=42
With this approach you'd not use conf.h file at all. Downside to this method is that you have to make sure that you remember to include this option for your both .c file compilations.
3. Include conf.h with compilation option
Example:
-include conf.h
This is similar to method 2, but instead of defining symbol directly, you would force the inclusion of the conf.h instead.
What ever you do, make sure your header files have include guards. (Thanks #ChristianGibbons)
The explanation is very simple.
First example (main)
in the conf.h you define it. When you include mylib.h it is already defined and it is not redefined. So the value is 42
In the second example (fn) you just include mylib.h and #ifndef condition is true.
Macros are expanded before the compilation and are valid only in one compilation unit.,

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

GCC module dependencies

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

Syntax error for time_t st_mtime line?

Why does this program produce a syntax error when built in Ubuntu?
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "time.h"
#include "sys/types.h"
#include "sys/stat.h"
int main()
{
time_t st_mtime;
printf("Hello\n");
return 0;
}
Here's what I get when I try to build this:
$ gcc -o test1 test1.c
test1.c: In function ?main?:
test1.c:10: error: expected ?=?, ?,?, ?;?, ?asm? or ?__attribute__? before ?.? token
test1.c:10: error: expected expression before ?.? token
Inspecting the preprocessor output:
$ gcc -E test1.c > test1.d
Shows line 10 as:
time_t st_mtim.tv_sec;
The error occurs only if I include both "sys/stat.h" & "time.h" files.
If you grep /usr/include for st_mtime, you'll find the following:
$ grep -r st_mtime /usr/include | grep define
/usr/include/x86_64-linux-gnu/bits/stat.h:# define st_mtime st_mtim.tv_sec
So the problem stems from your use of the variable name st_mtime.

Resources