Linking static libraries

Hi everybody! I am a beginner with C/C++ development. I have made my own sample library and I want to use it in my main file. All my code is:

Sum.h:
1
2
3
4
5
6
#ifndef _SUM_H_
#define _SUM_H_

int add(int n1, int n2);

#endif 



Sum.c:
1
2
3
4
5
6
#include "sum.h"

int suma(int n1, int n2)
{
     return n1 + n2;
}


main.c:
1
2
3
4
5
6
7
8
9
#include <stdio.h>
#include "sum.h"

int main(int argc, char* argv[])
{
	printf("%d", add(1, 2));	

	return 0;
}


For using the library I have written these commands:

gcc -c -o sum.o sum.c
ar rcs libsum.a sum.o
gcc main.c

My problem is that the last command, which compiles the main file, returns me that error:

/tmp/ccyhA7Bc.o: In `main' function:
main.c:(.text+0x1a): reference to `sum' undefined
collect2: error: ld returned 1 exit status

Where is my mistake? How can I fixed it?

Thanks.
two mistakes :
- c++ is case sensitive -> Sum.h not sum.h
- add in the header is add in the source file , not suma
gcc main.c -lsum
(That's a lower case 'L', not an upper case 'i'.)
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
#include "Sum.h"

int add(int n1, int n2) // not int suma(int n1, int n2)
{
     return n1 + n2;
}
Last edited on
Topic archived. No new replies allowed.