-2

I have a problem linking this simple C program using this makefile. I have included the lib named anagrammes.h in my makefile but the error seems to come from exemple.c

makefile :

all: exemple

exemple: exemple.o 

exemple.o: exemple.c anagrammes.h 
    gcc -o exemple.o -c exemple.c -Wall 

clean: 
    rm -rf *.o

exemple.c :

#include <stdio.h>
#include <string.h>
#include <anagrammes.h>

int main(void)
{

 int i; 
 char buf[]="chien";

 printf("\n signature:\n");
 printf("s(%s) = %s\n", buf, signature(buf));

 printf("\n anagramme:\n");
 anagramme(buf,0,strlen(buf));

 printf("\n getNiemeP:\n");

 for(i=1;i<=fact(strlen(buf));i++) {
     printf("%d %s\n",i,getNiemeP(buf, i));
 }

}

EDIT: Thanks everyone for helping me. Here is the error message that I've got :

Erreur:
exemple.c: In function ‘main’:
exemple.c:20:1: attention : contrôle a atteint la fin non void de la fonction [-Wreturn-type]
/tmp/ccPXm1wV.o: In function `main':
exemple.c:(.text+0x39): undefined reference to `signature'
exemple.c:(.text+0x98): undefined reference to `anagramme'
exemple.c:(.text+0xc2): undefined reference to `getNiemeP'
exemple.c:(.text+0x112): undefined reference to `fact'
collect2: ld a retourné 1 code d'état d'exécution
make: *** [exemple] Erreur 1

**I also have a static library named libannagrammes.a

How can I solve this ? Thanks.

user2360545
  • 131
  • 1
  • 2
  • 12
  • 1
    Presumably missing symbols for `anagramme` since `anagramme.c` is never compiled into `anagramme.o` and linked into `exemple. – Etan Reisner Jul 19 '15 at 19:05
  • Your `Makefile`(even corrected thanks to answers) does not *run* the program, it just compiles it. See [this](http://stackoverflow.com/a/16751650/841108) `Makefile` example. – Basile Starynkevitch Jul 19 '15 at 19:25
  • 2
    Note that `anagrammes.h` is not a library; it is a header. In a `makefile`, it is crucial that you understand the difference between a header and a library (and the difference between object files and libraries). You haven't linked with the code that implements the functions declared in the header. – Jonathan Leffler Jul 19 '15 at 20:07
  • 2
    Given the updated information in the question, you need to link with the library: add `-lannagrammes` to the linking command line after the object file `exemple.o`. You might need to add `-L .` before the `-lannagrammes` to ensure that the compiler searches in the current directory for the library. You might use: `exemple: exemple.o libannagrammes.a` followed by (tab-indented) `${CC} -o $@ ${CFLAGS} exemple.o -lannagrammes`, but you would do better with macros for both names so you can change them later if need be. – Jonathan Leffler Jul 19 '15 at 20:49
  • I think I understood what you're saying but what is the variable "CC" and what does mean $@ ? Sorry I'm new under linux and makefile making lol – user2360545 Jul 19 '15 at 21:08
  • Is `libanagrammes.a` a library in your current directory? Does this makefile need to build it? Is `anagrammes.h` in your current directory? Is `libanagrammes.a` in your current directory? – Etan Reisner Jul 19 '15 at 21:23
  • I could put them both in the same directory than exemple.c. The actual error that I have got is the one written in my first post. I don't know if I have to use this static lib or not, I just want to execute exemple.c – user2360545 Jul 19 '15 at 22:15

2 Answers2

3

Assuming the problem (since you failed to actually indicate the actual problem) is that your linking failed due to missing symbols then the solution is to modify the makefile and replace

exemple: exemple.o

with

exemple: exemple.o anagrammes.o

to instruct make that it needs to link both exemple.o and anagrammes.o into the exemple executable.

So you get a makefile like this:

all: exemple

exemple: exemple.o anagrammes.o

exemple.o: exemple.c anagrammes.h 
    gcc -o exemple.o -c exemple.c -Wall 

clean: 
    rm -rf *.o

That being said you don't need that exemple.o rule at all and can use the built-in rule for that too (as you do for exemple).

all: exemple

CFLAGS += -Wall

exemple: exemple.o anagrammes.o

exemple.o: exemple.c anagrammes.h

clean: 
    rm -rf *.o
Etan Reisner
  • 77,877
  • 8
  • 106
  • 148
  • I have edited my post. I included the error message and the fact that I have a static library named 'libannagrammes.a' . – user2360545 Jul 19 '15 at 20:30
1

You only added the header file annagramme.h, so the compiler knows the functions etc. declared there. However the lib is is either an *.o or a *.so (shared lib) that you will need to specify a compiler command line for exemple (specifying the libs and object files you want to link) in order to produce an executable. e.g.

all: exemple

exemple: exemple.o
    gcc -L<dir with libannagrammes.a> -o exemple exemple.o -lannagrammes 

exemple.o: exemple.c anagrammes.h 
    gcc -o exemple.o -c exemple.c -Wall 

clean: 
    rm -rf *.o

Note: if you have a lib that is not on your LIB_PATH you need to specify the location where to find it with the -L option (see also https://gcc.gnu.org/onlinedocs/gcc/Link-Options.html#Link-Options)

Gregor Ophey
  • 817
  • 6
  • 12
  • 1
    This is the right idea but neither `-lanagrammes` there or `anagrammes.o` in that rule are correct solutions to this problem. – Etan Reisner Jul 19 '15 at 19:19
  • I have edited my post. I included the error message and the fact that I have a static library named 'libannagrammes.a' . – user2360545 Jul 19 '15 at 20:30