0

I have a library called libdarph.a. I created it using

lib/libdarph.a: $(OBJS)
    mkdir -p ./lib
    ar qc $@ $^
    ranlib @

I also need two other libraries (libqhullstatic_r.a and libqhullcpp.a), located in a subdirectory different from ./lib, which I compiled previously using another makefile:

./src/qhull/lib/libqhullstatic_r.a:
    make -C ./src/qhull/

./src/qhull/lib/libqhullcpp.a:
    make -C ./src/qhull/

One of the files in $(OBJS), needed in libdarph.a uses code based on libqhullcpp.a and libqhullstatic_r.a. Therefore I compiled it using:

./src/DichotomicSearch.o: ./src/qhull/lib/libqhullstatic_r.a ./src/qhull/lib/libqhullcpp.a
    $(CCC) $(CCFLAGS) -c -I./inc/ -I./src/qhull/ -L./src/qhull/lib -lqhullcpp -lqhullstatic_r -lm ./src/DichotomicSearch.cpp -o ./src/DichotomicSearch.o 

I don't have any problems creating the library libdarph.a. But when I want to use it, to create a binary, e.g.

binary: $(BINARY_SRC) 
    $(CCC) $(CCFLAGS) $(BINARY_SRC) -I./inc/ -I./src/qhull/ -L./lib -L./src/qhull/lib -lqhullcpp -lqhullstatic_r -ldarph -lm -o $(BINARY_EXE) 

I receive the following error:

/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: ./lib/libdarph.a(DichotomicSearch.o): in function `DichotomicSearch::test_3()':
/home/gaul/ownCloud/VSProjects/dynamic-e.-b.-milp/./src/DichotomicSearch.cpp:102: undefined reference to `orgQhull::Qhull::Qhull()'
/usr/lib64/gcc/x86_64-suse-linux/7/../../../../x86_64-suse-linux/bin/ld: /home/gaul/ownCloud/VSProjects/dynamic-e.-b.-milp/./src/DichotomicSearch.cpp:102: undefined reference to `orgQhull::Qhull::~Qhull()'
collect2: error: ld returned 1 exit status

I can't make any sense of why orgQhull::Qhull::Qhull() is not found (it is included in libqhullcpp.a), because I included the flags -lqhullcpp and -lqhullstatic_r in the lines where the object DichotomicSearch.o is created.

Does someone know how to change my Makefile to make this work? Help is very much appreciated!

  • 1
    If `libdarph.a` uses `libqhullcpp.a` then `libqhullcpp.a` needs to be listed after `libdarph.a` in the linker command – Alan Birtles Aug 22 '22 at 15:41
  • 1
    Firstly there is no point in using linker options when you are only compiling, so remove `-L` and `-l` from your compilation commands e.g. `./src/DichotomicSearch.o: ./src/DichotomicSearch.cpp $(CCC) $(CCFLAGS) -c -I./inc/ -I./src/qhull/ ./src/DichotomicSearch.cpp -o ./src/DichotomicSearch.o` – john Aug 22 '22 at 15:41
  • 1
    Secondly the order of the libraries in your actual link is wrong. `libdarph.a` needs to be before the libraries it references. If you have cyclic references then you can list a library twice. – john Aug 22 '22 at 15:41

0 Answers0