0

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:

enter image description here

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"
...

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Indeed, I forgot to add that there, thanks. But now I'm getting this error: gcc-9: error: semaphore_v2.o: No such file or directory... I think compilation is good and also are the files... any clue?
Look at my answer, and note you should add semaphore_v2.o twice to the compilation target (one time as dependency and one time as input to the gcc command). I think you added it only once, and this causes the problem you described.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.