Syntax error for time_t st_mtime line? - c

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.

Related

undefined reference to 'XXX' after using #define _GNU_SOURCE

I used search.h for my c program, where I need to put #define _GNU_SOURCE to the first line in order to introduce multiple hash tables. But after that, errors like undefined reference to 'log10' and undefined reference to 'PQntuples' popped up. I certainly need all the packages there, how should I now compile the program? Any help would be deeply appreciated! Thanks.
The headers:
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <math.h>
#include <string.h>
// library for psql
#include <libpq-fe.h>
#include <unistd.h>
#include <time.h>
#include <search.h>
int main(void){
char host[] = "localhost";
char port[] = "5432";
char db_name[] = "db_name";
char user[] = "test_usr";
char password[] = "123456";
sprintf(db_str, "host=%s port=%s dbname=%s user=%s password=%s",
host, port, db_name, user, password);
PGconn *db_connection = DBConnect(db_str);
struct hsearch_data htab;
hcreate_r(10, &htb);
ENTRY e, *ep;
e.key = "test";
e.data = (void *) 1;
hsearch_r(e, ENTER, &ep, &htab);
}
And this was how I compile the file:
gcc -Wall -Wextra -I/home/userX/postgresql/include -L/home/userX/postgresql/lib -lm -lpq -g my_program.c
Specifiy the libraries at the end of the command line
gcc -Wall -Wextra -I/home/userX/postgresql/include \
-L/home/userX/postgresql/lib -g my_program.c -lpq -lm
// ^^^^^^^^
gcc looks at required symbols left to right on the command line.
With the libraries before the source files, when gcc processes the -llib argument it does not have any requirements and therefore does not "extract" any function from the library.

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>
}

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

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

Resources