I have the below tree:
├── cminpack
│ ├── hybrd.c
│ ├── hybrd.o
│ ├── hybrj1.c
│ ├── hybrj1.o
│ ├── hybrj.c
│ ├── hybrj.o
│ ├── libminpack.a
│ ├── Makefile
│ ├── minpack.h
│ ├── minpack.h.gch
│ ├── readmeC.txt
│ ├── readme.txt
├── greeter.c
├── greeter.h
├── greeter.o
├── swaps.c
├── swaps.h
├── swaps.o
The files:
cminpack
is a package I found and copied and I canmake
it successfullyswaps.c
is where mymain
is, I also do have the below in it:
#include "./cminpack/minpack.h"
#include "./greeter.h"
greeter.c
is another file with some functions
In swaps.c
I call a function from greeter.c
and function hybrd_
from cminpack/hybrd.c
. I want to link both greeter
and cminpack
to my swaps
.
I manage to link greeter
by running the below gcc command:
sudo gcc -c swaps.c greeter.c ;sudo gcc -o swaps swaps.c greeter.c -lm ;./swaps
Now if I try to link cminpack
I am running the below gcc command:
sudo gcc -c swaps.c greeter.c -I./cminpack;sudo gcc -o swaps swaps.c greeter.c -lm -L /cminpack
This leads to the infamous linking error:
/usr/bin/ld: /tmp/ccxwxt6A.o: in function `fsolve':
swaps.c:(.text 0xfb3): undefined reference to `hybrd_'
collect2: error: ld returned 1 exit status
How do I link the subfolder?
(gcc version 10.2.1 20210110 (Debian 10.2.1-6))
CodePudding user response:
In addition to -L cminpack
, you should add -lminpack
to the gcc
command line.
There should be no reason to use sudo
for the compilation of this program, if you cannot invoke gcc
directly, your system is misconfigured and should be fixed. Running the compiler as root is highly discouraged.
Create a Makefile
in the main directory with these lines:
%.o: %.c greeter.h swaps.h cminpack/minpack.h
gcc $(CFLAGS) -I cminpack -o $@ $*.c
swap: swap.o greeter.o cminpack/libminpack.a
gcc $(CFLAGS) -o $@ swaps.o greeter.o -L cminpack -lminpack -lm
cminpack/libminpack.a:
make -C cminpack