-1

I'm experimenting with linking and sharing header files in C; I would like to share this function, tool.c, between many programs:

void sayHello() {
    printf("Hello!");
}

...I define the function in tool.h:

void sayHello();

It makes a lot of sense if other programs #include tool.h so that they can all call the sayHello function. I need to create a single program from multiple files. Here I can create an entirely different program (target) that calls the function:

#include <stdio.h>
#include "/usr/someone/somewhere/tool.h"

int main() {
    sayHello();
}

Though, the linker process fails: linker command failed with exit code 1. If you wish, download the Xcode project here.

What am I doing wrong?

Cesare
  • 9,139
  • 16
  • 78
  • 130
  • Are you linking with `tool.c`? Also it would be useful to post the error message rather than an image. – Filipe Gonçalves Aug 23 '15 at 11:59
  • Thanks @FilipeGonçalves! What do you mean by "are you linking with"? I appreciate your help. – Cesare Aug 23 '15 at 12:04
  • 1
    It's not enough to include a header file with the function declaration - you need to tell the linker where to find the code for `sayHello()`. In the command line, you typically do this by indicating the additional files when invoking gcc, e.g. `gcc myprog.c tool.c` (or if `tool.c` is already compiled as a library, you indicate the corresponding library file). I don't know how to do it in XCode, but you can compile `tool.c` into a static library and then add it to the project dependencies - see http://stackoverflow.com/questions/15973472/adding-static-libraries-to-a-c-based-project-with-xcode – Filipe Gonçalves Aug 23 '15 at 12:10
  • So, first you create a static library (the way you do this is by creating an object file for `tool.o` with gcc and then use `ar(1)` to create the `.a` library - see http://stackoverflow.com/questions/2734719/how-to-compile-a-static-library-in-linux). Then you include `tool.h` when you need to use it. Then you add the `.a` library file to the project dependencies list in XCode as shown in that question I mentioned before. – Filipe Gonçalves Aug 23 '15 at 12:14
  • Thanks a lot for your explanation. It is really helpful. The problem is that Xcode won't give the executable file until the program compiles. Since I have some errors, Xcode doesn't compile my C files. The error is `linker command failed with exit code 1` so there seems to be an issue with the linker process - just as you suggested. Have you got any tips Thanks! – Cesare Aug 23 '15 at 12:14
  • Yes, you should compile the library first, I'd say outside of XCode, so that you can get a standalone `.a` library file to add to the project dependencies (then you should be able to build the project). Use command-line tools, namely `gcc` and `ar`. – Filipe Gonçalves Aug 23 '15 at 12:16
  • Do not post pictures if not really justified! – too honest for this site Aug 23 '15 at 13:00

1 Answers1

1

The problem is not the header file in itself; while you included the header, you did not tell the linker where to find the actual definitions of your code. So you need to compile the .c file e.g. into a static library and add that to the linker options of your other_tool.

anderas
  • 5,744
  • 30
  • 49