0

I am trying to install a C software and then make a test program that uses it.

The software consists of three files:

  1. x-as-func.c
  2. x-as-func.h
  3. y.h (this has only define statements)
  4. the executable x

In the makefile I have a "install" command as:

 .PHONY: install
 install: all
          cp ./include/x-as-func.h /usr/local/include/x.h
          cp ./include/y.h /usr/local/include/y.h
          cp ./x /usr/local/bin/x

When I do "sudo make install" everything runs fine and the files are copied as expected.

However when I compile the test program that looks like:

test.c

 #include "hubo-read-trajectory-as-func.h"

 int main(){
         char* s ="left_elbow.txt";
         someFunctionDefinedInX(s, 0, false, false);
 }

I get error:

 gcc -o test test.c -include  //usr/local/include/x.h
 /tmp/cc6ru89m.o: In function `main':
 test.c:(.text+0x27): undefined reference to `someFunctionDefinedInX'
 collect2: ld returned 1 exit status

Or if I do -ld then I get

 gcc -o test random-test.c -ld x.h
 gcc: error: x.h: No such file or directory

I believe that the header files and the executables are not getting linked? Am I right? How should I correct this?

Manas Paldhe
  • 766
  • 1
  • 10
  • 32

3 Answers3

0

You need to compile to an object file using the -c switch for gcc. This performs compilation but does not link your program, meaning that you can have a reference (such as the someFunctionDefinedInX) which is not yet resolved. Later, when you need to perform final compilation and link in that reference, use gcc myFirstUnlinkedObj.o mySecondUnlinkedObj.o. This will look at all of the object files, find all of the undefined references, and look them up in the other object (or source, if any) files. It then links them all together in your final executable.

You can find a good explanation of what an object file is in this question.

A makefile which could perform these steps for you could look like this (minus your install target):

x-as-func.o:
       gcc -c x-as-func.c

my-exe-file: x-as-func.o
       gcc main-file.c x-as-func.o -o my-exe-file

all: my-exe-file

This is assuming, of course, that all of your .c files exist in the same directory as your makefile.

Community
  • 1
  • 1
Chris Hayes
  • 11,471
  • 4
  • 32
  • 47
0

You have two different problems.

The first is that you din't tell the linker to link with the newly installed library. For that you have to use the -L and -l (small L) options.

The second problem is that /usr/local/include is not in the default search path for header files, and you need to use the -I option to add it.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • how do I solve the first one? Do I add -L or -l while compiling the "software" that is used by test? – Manas Paldhe Sep 03 '13 at 06:00
  • @ManasPaldhe You add the correct options to the compilation/linking, e.g. `-L/usr/local/lib -lyour_library`. – Some programmer dude Sep 03 '13 at 06:02
  • I am sorry, but it is still not clear. Can you explain the answer in detail? It is not clear to me. So the software installation is correct and I need to compile the test properly or is the software installation also incorrect? – Manas Paldhe Sep 03 '13 at 06:03
  • if you mean "gcc -o test random-test.c -L /usr/local/lib/ -l x.h" then I am still getting the same error – Manas Paldhe Sep 03 '13 at 06:04
  • @ManasPaldhe You need to **link** with the file containing the definition of the missing function. Either your project should create an object file or a library that you need to link with. It seems the installation procedure of your project is not finished. – Some programmer dude Sep 03 '13 at 06:13
  • Could you kindly let me know how do I create a object file or a library that I can work with? Thank you. – Manas Paldhe Sep 03 '13 at 06:14
  • I believe http://stackoverflow.com/questions/2734719/howto-compile-a-static-library-in-linux should help me. Although, I did not get how should I install the created library? Just copy it? – Manas Paldhe Sep 03 '13 at 06:19
  • @ManasPaldhe Yes, just copy it. Then use e.g. `-lout` (name taken from answer in other question) to link with it. – Some programmer dude Sep 03 '13 at 06:23
0

I don't get your specifics but .h in compiler invocation seems wrong, to start with. From info you provided i see two options:

  1. functions you need are inlined in header file. In that case, you just need to include it into your .c file.
  2. your functions defined in .c files that you haven't linked into library (at least from your example i don't see that you have a library). This had to be fixed - create a library (static/dynamic - whatever you prefer, depends on what you want to achieve). Getting function from another executable file isn't something you need, unless you have good experience with dynamic linking
keltar
  • 17,711
  • 2
  • 37
  • 42
  • Yes, the function I need are inlined in header. But I have never seen a .c file being included when using a installed repository. – Manas Paldhe Sep 03 '13 at 06:12
  • How do I create a library? Say a static library. – Manas Paldhe Sep 03 '13 at 06:13
  • #include "some_file.h". -Ipath/to/dir (it's capital 'i') will add this directory to search path. However, from what i see i'm not quite sure your functions are inlined (what do you need x-as-func.c for then?). As for static library (which you don't need _if_ your functions are inlined - there is just nothing to include into library), there is so many tutorials about that... The real question here is why you ever wanted to install your code into system directories at first place? – keltar Sep 03 '13 at 06:21
  • Oh! Sorry, I got it wrong. The header just has the function declaration. Not the implementation. – Manas Paldhe Sep 03 '13 at 06:22
  • Then compile all .c files that you need functions from into a library (say, libfoo.a), include header and link with library (-Lpath/to/library/directory -lfoo). http://www.yolinux.com/TUTORIALS/LibraryArchives-StaticAndDynamic.html check this out. – keltar Sep 03 '13 at 06:27