Issue compiling with a new module on Linux - c

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.

Related

linux/if_tun.h on Windows (mingw64)

How can I fix this error in mingw64?
gcc -O2 -Wall -g -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -MMD -D_GNU_SOURCE -DCONFIG_VERSION=\"2019-12-21\" -DCONFIG_SLIRP -DCONFIG_FS_NET -DCONFIG_SDL -DCONFIG_RISCV_MAX_XLEN=128 -DCONFIG_X86EMU -c -o temu.o temu.c
temu.c:39:10: fatal error: linux/if_tun.h: No such file or directory
39 | #include <linux/if_tun.h>
| ^~~~~~~~~~~~~~~~
compilation terminated.
make: *** [Makefile:129: temu.o] Error 1
I'm trying to compile tinyemu.
It turns out I need to remove everything in a "#ifndef" area that is not supposed to be used on Windows.
#ifndef _WIN32
#include <termios.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <linux/if_tun.h>
#endif

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.

undefined reference to something for what I have a library for

When compiling my school project written in C I get following errors:
/tmp/ccFDQk9j.o: In function `main':
proj2.c:(.text+0x187): undefined reference to `sem_open' (several times)
proj2.c:(.text+0x35a): undefined reference to `sem_post' (several times)
proj2.c:(.text+0x381): undefined reference to `sem_wait' (several times)
proj2.c:(.text+0x6ec): undefined reference to `sem_close' (several times)
proj2.c:(.text+0xda6): undefined reference to `sem_unlink' (several times)
My libraries are:
#include <errno.h>
#include <fcntl.h>
#include <semaphore.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/wait.h>
#include <unistd.h>
#include <string.h>
#include <sys/sem.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <time.h>
And I am compiling using:
gcc -std=gnu99 -Wall -Wextra -Werror -pedantic proj2.c -o ext
Any ideas?
(Also I am ashamed to admit I put everything into one main. Yeah. Please don't stone me.)
EDIT:
So I adjusted my compilation settings to:
gcc -std=gnu99 -Wall -Wextra -Werror -pedantic -pthread proj2.c -o ext
And the amount of complaints has been reduced to these only two:
/tmp/cc51XZFK.o: In function `main':
proj2.c:(.text+0x249): undefined reference to `shm_open'
proj2.c:(.text+0xdf9): undefined reference to `shm_unlink'
collect2: ld returned 1 exit status
EDIT 2.0:
Same error mistake for
gcc -std=gnu99 -lrt -Wall -Wextra -Werror -pedantic -pthread proj2.c -o ext
EDIT 3.0:
Successfully compiled. Thank you Paul Griffiths and Joachim Isaksson.
gcc -std=gnu99 -Wall -Wextra -Werror -pedantic proj2.c -pthread -lrt -o ext
For sem_* functions, link with -pthread.
For shm_* functions, link with -lrt.
Probably you need to link with a library where those sem_* methods are.
-lpthread

unknown type name 'uint32_t', MinGW

I get the error unknown type name 'uint32_t' and included stdint.h. uint8_t doesn't produce an error, neither does uint16_t.
I'm using MinGW and the following make-lines:
# Build for Windows under MinGW
#MINGWDBG= -DDEBUG -O0
MINGWDBG= -DNDEBUG -Os
#MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,console $(MINGWDBG) -DHAVE_STDINT
MINGWOPT= -W -Wall -mthreads -Wl,--subsystem,windows $(MINGWDBG)
mingw:
windres win32\res.rc win32\res.o
gcc $(MINGWOPT) mongoose.c -lws2_32 \
-shared -Wl,--out-implib=$(PROG).lib -o $(PROG).dll
gcc $(MINGWOPT) mongoose.c main.c win32\res.o -lws2_32 -ladvapi32 \
-o $(PROG).exe
Code:
uint32_t function(void) {
return VALUE;
}
And the includes:
#include <stdio.h>
#include <string.h>
#include "mongoose.h"
#include "main.h"
#include <stdint.h>
To answer my own question, changing the order of the includes seemed to do the trick.
If
#include "main.h"
#include <stdint.h>
didn't work, but
#include <stdint.h>
#include "main.h"
did, it is likely that your main.h file relies on stdint.h. That means you should add #include <stdint.h> to main.h.

Resources