Frage

I am trying to write a makefile for a small project which uses GTK libraries.

# Compiler
cc = gcc

#Options for Development
CFLAGS = `pkg-config --cflags --libs gtk+-2.0`

all: pss

pss : main.o interface.o
#   $(cc) $(CFLAGS) -o pss main.o interface.o

main.o : main.c interface.h
interface.o : interface.c

pss is supposed to be the final executable file. However, the makefile does not create the executable pss. When I explicitly add the line for creating pss, then I am getting a linking error.

asheesh:~/Source$ make
gcc `pkg-config --cflags --libs gtk+-2.0` -o pss main.o interface.o
interface.o: In function `interface':
interface.c:(.text+0x1e): undefined reference to `gtk_init'
interface.c:(.text+0x28): undefined reference to `gtk_window_new'
interface.c:(.text+0x38): undefined reference to `gtk_widget_show'
interface.c:(.text+0x3d): undefined reference to `gtk_main'
collect2: ld returned 1 exit status
make: *** [pss] Error 1

How do I create the final executable file using make?

Changed the makefile to handle library dependencies properly. Still not working.

#Options for Development
CFLAGS = `pkg-config --cflags gtk+-2.0`

#Libraries
LIBS   = `pkg-config --libs gtk+-2.0`

all: pss

pss : main.o interface.o
    $(cc) $(LIBS) $(CFLAGS) -o pss main.o interface.o

main.o : main.c interface.h
    $(cc) $(CFLAGS) -o main.o main.c interface.o


interface.o : interface.c
    $(cc) $(CFLAGS) $(LIBS) -o interface.o interface.c
War es hilfreich?

Lösung 2

I am not entirely sure what the problem was, but it got solved by manually specifying the compile commands.

# Compiler
cc = gcc

#Options for Development
CFLAGS = `pkg-config --cflags gtk+-2.0`

#Libraries
LIBS   = `pkg-config --libs gtk+-2.0`

all: pss

pss : main.o interface.o
    $(cc) -o pss main.o interface.o $(LIBS)
#   $(cc) $(LIBS) -o pss main.o interface.o
#   This line fails to link whereas the line above works.

main.o : main.c interface.h
    $(cc) $(CFLAGS) -c main.c interface.c

interface.o : interface.c
    $(cc) $(CFLAGS) -c interface.c

Andere Tipps

Looks like your compilation phase is not able to find include files. When I see how you built your .o files, there is no -I flag to indicate where to find gtk include files.

You may want to add suffix rules:

.SUFFIXES: .c .o
.c.o:
        gcc -c `pkg-config --cflags gtk+-2.0` $<

You've forgotten '-c' option while building .o files (in your first message). You also don't need to include *.o or *.c files in compile phase:

$(cc) $(CFLAGS) -o main.o main.c interface.o // wrong
$(cc) $(CFLAGS) -c -o main.o main.c // right

and to your last code:

# Compiler
CC = gcc // Fixed: CAPSLOCK here

#Options for Development
CFLAGS = `pkg-config --cflags gtk+-2.0`

#Libraries
LIBS   = `pkg-config --libs gtk+-2.0`

all: pss

pss : main.o interface.o
    $(cc) -o pss main.o interface.o $(LIBS)
#   $(cc) $(LIBS) -o pss main.o interface.o
#   This line fails to link whereas the line above works.

main.o : main.c interface.h
    $(CC) $(CFLAGS) -c main.c

interface.o : interface.c
    $(CC) $(CFLAGS) -c interface.c

Also you can write 5 last lines even simpler:

main.o : main.c interface.h

interface.o : interface.c

No recipes needed because of the implicit rule for building .o from .c with recipe in a form:

    $(CC) $(CPPFLAGS) $(CFLAGS) -c
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top