1

I'm having a couple of issues including custom libraries in my program

I have my main.c file and a library.c (where all the functions are stored) and library.h (where all the prototypes are stored).

In main.c I'm placing #include "library.h" but the functions don't get recognized when I try to compile.

Am I doing something wrong?

I'm using GCC to build the file.

test.c:

#include "library.h"

int main()
{
    int num = 5;
    sum(num);
}

library.c

#include "library.h"


int sum(int num)
{
    return num + 5;
}

library.h

#ifndef LIBRARY_H
#define LIBRARY_H

#include <stdio.h>

int sum(int num);

#endif

Getting error:

C:\Users\Gabriel\Desktop\test.o:test.c|| undefined reference to `sum'|

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
gabevt
  • 33
  • 1
  • 7
  • What is the exact error message you're seeing? – Dilip Kumar Nov 20 '15 at 06:28
  • did you supply the include path using `-I` option during compilation time? – Dilip Kumar Nov 20 '15 at 06:28
  • I have all the files on the same folder, error message is "undefined reference to " with all of my functions. I didn't make use of the -I parameter, I wasn't aware it existed – gabevt Nov 20 '15 at 06:31
  • Show the sample files, and compilation statements – Dilip Kumar Nov 20 '15 at 06:32
  • Most of my code is in spanish, would that be an issue? – gabevt Nov 20 '15 at 06:34
  • 2
    According to the description in the question, you are doing everything right. But it doesn't work, so ... you need to make an [MCVE](http://stackoverflow.com/help/mcve) that demonstrates the problem. – user3386109 Nov 20 '15 at 06:36
  • Added the MCVE to the question – gabevt Nov 20 '15 at 06:43
  • 3
    "Undefined reference" is a linker error. What is the **complete** command you're using to **compile**? – Darwin von Corax Nov 20 '15 at 06:43
  • Just using the build/run in codeblocks – gabevt Nov 20 '15 at 06:45
  • 1
    Including just the header file won't link the library automatically. Make lib.c is included with your project, so that both files get compiled *and* linked together. –  Nov 20 '15 at 06:46
  • @Evert Could I do this without making a project? – gabevt Nov 20 '15 at 06:51
  • Without a project: see harper's answer. That's pretty much the command line solution. –  Nov 20 '15 at 07:17
  • @DarwinvonCorax Humans are not always 100% accurate. Usually GCC is named as the GNU C compiler (omitting "suite"), and indeed GCC compiles the source files altough it does more unless not restricted with command arguments. With the style of question you wouldn't expect a sophisticated problem, so you can assume that the `GCC build` doesn't differentiate a compile commmand from any link command. – harper Nov 23 '15 at 11:33
  • That 's pretty much what I was asking, although you worded it better. – Darwin von Corax Nov 23 '15 at 16:02

1 Answers1

6

Including the header file is not sufficient. The prototype in the header file just tells the compiler that there should be a function.

You have to actually add the function to the program. That is done when linking. the simplest way would be

gcc -o myprog test.c library.c

There are more sophisticated option. If you want to add several files actually to a library you can compile them independently and build the archive. Here are some commmands that show the basic idea.

gcc -o library.o library.c
gcc -o someother.o someother.c
ar a libmy.a library.o someother.o

gcc -o myprog test.c -l my
harper
  • 13,345
  • 8
  • 56
  • 105