Add linux headers in gcc command - c

So I'm trying to include some headers in a c file on ubuntu16.04.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <fcntl.h>
#include <errno.h>
#include "list.h"
#include "phypages.h"
#include "pagetable.h"
It has the error:
fatal error: list.h: No such file or directory
I found all linux header files are in the
/usr/src/linux-headers-4.4.0-164/include/linux
So I use -I to include
gcc XXX.c -o XXX -I/usr/src/linux-headers-4.4.0-164/include/linux
But then has the error
/usr/src/linux-headers-4.4.0-164/include/linux/stddef.h:4:31: fatal error: uapi/linux/stddef.h: No such file or directory
Can anyone help? Thank you!

Suggest using;
gcc XXX.c -o XXX -I/usr/include -I/usr/src/linux-headers-4.4.0-164/include/linux
so the 'normal' C header files are searched before searching the linux header files

Maybe it #include<list.h>, not #include"list.h"?

Related

one directory for c file which must compile first without main in funcs/ directory& c file wth main function &common header file in separate directory

I have two source files and common header file. header file is like following
#ifndef TEST
#define TEST
#include <unistd.h>
#include <stdio.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <time.h>
#define PORT 5000
#define BUF_SIZE 20000
#define MAIN "GET / HTTP/1.1" //initial
#define REQUEST "GET /add?"//with query
#define SOCKET_BUFFER 2048
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#define MAIN_RETURN 2
#define CSS_RETURN 3
#define REQUEST_RETURN 4
int SET_MAIN=0;
int ads_today=0;
int process(int size,char buffer[size],char status);
int min(int a, int b);
ssize_t sendfile(int out_fd, int in_fd, off_t *offset, size_t count);
#endif
I am getting error that sounds like I am including my common header file twice. common header file exists in ./headers/ directory. main function source file which contains main function is in ./ directory which is root of small project
My make file is like following
FUNCS = funcs/
HEADERS = headers/
HEADER_FROM_FUNCS_DIR=../$(HEADERS)
lh : $(FUNCS)test_func.o test.o
cc -o lh $(FUNCS)test_func.o test.o
test_func : $(FUNCS)test_func.c $(HEADERS_FROM_FUNCS_DIR)/test.h
cc -c $(FUNCS)test_func.c
test.o : test.c $(HEADERS)/test.h
cc -c test.c
clean :
rm lh $(FUNCS)test_func.o test.o
since I have defined #ifndef directive in .h file so I really don't know why my both source files including .h in lh executable which is my final program supposed to be in root of project directory result of user #: make command
project directory structure
./test.c #with main function
./headers/test.h #
./funcs/test_func.c #without main function, must be compiled first which is supporting file with functions
Can I fix this error, thanks
Also How not to hard code file names in Makefile. I really don't mind keeping names for output files but I really hate source file names in Makefile that I must hard code names of source files and header files in Makefile, Is there a way to get rid of hardcoded file names like use some sort of built-in Make flags that will help compile source files with any names and include header files
make outputs
version_1# make
cc -c -o funcs/test_func.o funcs/test_func.c
cc -c test.c
cc -o lh funcs/test_func.o test.o
/usr/bin/ld: test.o:(.bss+0x0): multiple definition of `SET_MAIN'; funcs/test_func.o:(.bss+0x0): first defined here
/usr/bin/ld: test.o:(.bss+0x4): multiple definition of `ads_today'; funcs/test_func.o:(.bss+0x4): first defined here
collect2: error: ld returned 1 exit status
make: *** [Makefile:6: lh] Error 1

Issue compiling with a new module on Linux

I'm trying to compile a project that contains different modules (.h files) so I'm using a makefile. I added a new module called semaphore_v2.h which I included in another module called connection.h. connection.h is included into jack.c where the main process is. When compiling using make command, I get the following error:
Seems like despite I included the module, it doesn't keep track of the references of the semaphore_v2.h module and I can't find the issue here. What's wrong?
makefile:
all: compilation
jack.o: jack.c
gcc -c jack.c -Wall -Wextra
semaphore_v2.o: semaphore_v2.c semaphore_v2.h
gcc -c semaphore_v2.c -Wall -Wextra
files.o: files.c files.h
gcc -c files.c -Wall -Wextra
connection.o: connection.c connection.h
gcc -c connection.c -Wall -Wextra
compilation: jack.o files.o connection.o
gcc jack.o connection.o files.o -o jack -Wall -Wextra -lpthread
connection.h
#ifndef _CONNECTION_H_
#define _CONNECTION_H_
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <time.h>
#include "files.h"
#include <ctype.h>
#include <pthread.h>
#include <sys/wait.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include "semaphore_v2.h"
typedef struct {
int socketfd;
int memid;
semaphore *sinc;
}thread_input;
...
jack.c
#include <signal.h>
#include "connection.h"
...
You should change your makefile compilation target to include also semaphore_v2.o
so you should have such a line at your makefile:
compilation: jack.o files.o connection.o semaphore_v2.o
gcc jack.o connection.o files.o semaphore_v2.o -o jack -Wall -Wextra -lpthread
Note semaphore_v2.o appears twice, one time as a dependency to this target and one time as input to the gcc command.

WIFCONTINUED not recognised

I'm writing a C program and GCC doesn't recognise WIFCONTINUED. I've included the library that contains it (sys/wait.h) and checked that the library does exist on my machine.
My program:
#include <stdio.h>
#include <linux/limits.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
#include "LineParser.h"
//Some code...
if(WIFCONTINUED(status)){
temp->status = RUNNING;
}
//More code...
The error I get when I compile is:
warning: implicit declaration of function ‘WIFCONTINUED’; did you mean ‘__W_CONTINUED’? [-Wimplicit-function-declaration]
else if(WIFCONTINUED(status)){
^~~~~~~~~~~~
__W_CONTINUED
undefined reference to `WIFCONTINUED'
collect2: error: ld returned 1 exit status
Has anyone experienced a similar problem with WIFCONTINUED?
EDIT:
Here is some example(not my original program) that failes to compile with the same errros:
#include <stdio.h>
#include <linux/limits.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <string.h>
int main(int argc,char** argv){
int status;
if(WIFCONTINUED(status)){
printf("Works");
}
return 0;
}
My OS is Ubuntu 18.04 and my makefile is:
all: rep
rep: rep.o
gcc -g -Wall -m32 rep.o -o rep
rep.o: rep.c
gcc -g -Wall -m32 -ansi -c -o rep.o rep.c
.PHONY : clean
clean :
-rm -f *.o
The cause is the -ansi flag. It's equivalent to -std=c90 and WIFCONTINUED is not part of the C standard.
Most of the system programming headers you have included are from POSIX. So -ansi isn't going to help with anything at all. Just dropping the flag -ansi would be sufficient to fix this.

Undefined reference to 'owr_init' (linker error)

Output of make all:
make all
Building file: ../webrtc.c
Invoking: Cross GCC Compiler
gcc -std=c99 -I/opt/openwebrtc-0.3/include/ -I/usr/include/glib-2.0 -I/usr/lib/x86_64-linux-gnu/glib-2.0/include -O0 -g3 -Wall -c -fmessage-length=0 -ansi -MMD -MP -MF"webrtc.d" -MT"webrtc.o" -o "webrtc.o" "../webrtc.c"
Finished building: ../webrtc.c
Building target: WebRTC
Invoking: Cross GCC Linker
gcc ./webrtc.o -lglib-2.0 -lsctp -o "WebRTC" -L/opt/openwebrtc-0.3/lib -L/usr/lib/x86_64-linux-gnu/glib-2.0/include -L/usr/include/glib-2.0
./webrtc.o: In function `startServer':
/home/sn/workspace/cpp/praktikum/WebRTC/Debug/../webrtc.c:17: undefined reference to `owr_init'
/home/sn/workspace/cpp/praktikum/WebRTC/Debug/../webrtc.c:18: undefined reference to `owr_run_in_background'
collect2: error: ld returned 1 exit status
makefile:32: recipe for target 'WebRTC' failed
make: *** [WebRTC] Error 1
webrtc.c:
#include "webrtc.h"
int main(void)
{
/*startClient();*/
startServer();
return EXIT_SUCCESS;
}
void startServer(void)
{
printf("Initializing OpenWebRTC");
owr_init(NULL);
owr_run_in_background();
}
webrtc.h:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <ctype.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <pthread.h>
#include <owr/owr.h>
#include <owr/owr_data_channel.h>
#include <owr/owr_crypto_utils.h>
#include <owr/owr_types.h>
/* Defines:*/
#define MAX_BUFFER 1024
#define SERVER_PORT_NUMBER 65300
#define SERVER_BIND_ADDR "127.0.0.1"
/* Prototypes: */
void startServer(void);
Output of file:
libopenwebrtc.so.4201.0.0: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, BuildID[sha1]=3a9726e870736687b14c1dca326bda336fb2d088, stripped
I've build OpenWebRTC as stated in the wiki located here on Debian 8 and installed the generated .deb files. In Eclipse I've included the header files (/opt/openwebrtc-0.3/include) and the libs (/opt/openwebrtc-0.3/lib) and it is still not working. I've also uploaded the generated deb files into my apt repository
sources.list:
deb http://openwebrtc.niehus.eu/apt/debian/ jessie main
Try to change
gcc ./webrtc.o -lglib-2.0 -lsctp -o "WebRTC" -L/opt/openwebrtc-0.3/lib -L/usr/lib/x86_64-linux-gnu/glib-2.0/include -L/usr/include/glib-2.0
to
gcc ./webrtc.o -lglib-2.0 -lsctp -lopenwebrtc -o "WebRTC" -L/opt/openwebrtc-0.3/lib -L/usr/lib/x86_64-linux-gnu/glib-2.0/include -L/usr/include/glib-2.0
The problem might be that you are not linking necessary library.

ERROR: ld.so: object 'getpid.so' from LD_PRELOAD cannot be preloaded: ignored

When I try to use LD_PRELOAD as following,
LD_PRELOAD=getpid.so ./testpid
I get the following error...
ERROR: ld.so: object 'getpid.so' from LD_PRELOAD cannot be preloaded: ignored.
I compile getpid.so by using
gcc -Wall -fPIC -shared -o getpid.so getpid.c
and it contains the following code...
// getpid.c
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
pid_t getpid(void)
{
printf("Hello, world!\n");
return syscall(SYS_getpid);
}
tespid.c constains code which uses getpid as shown below and which is compiled by doing
gcc testpid -o testpid.c
What can be the problem here? Why is LD_PRELOAD not working?
// testpid.c
#include <sys/syscall.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
int main()
{
printf( "pid = %d!\n", getpid() );
return 0;
}
Looks like the loader is unable to find getpid.so as you've not mentioned the path to the library.
Try:
LD_PRELOAD=/full/path/to/getpid.so ./testpid
I was also facing error as below
ERROR: ld.so: object '/usr/lib64/libjemalloc.so.1' from LD_PRELOAD cannot be preloaded
Below were my steps resolved the error for me.
Go to the path /usr/lib64 and look for the libjemalloc using below commands*
#cd /usr/lib64
#ls | grep libjemalloc
if you don't find you don't have that package installed in your system
$sudo yum whatprovides libjemalloc*
$sudo yum install jemalloc-3.6.0-1.amzn2.x86_64

Resources