如何将子文件夹与C中的GCC联系起来?

发布于 2025-01-30 01:43:27 字数 1578 浏览 4 评论 0原文

我有以下树:

├── 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

文件:

  • cminpack是我找到并复制的软件包,我可以成功地make它成功地
  • swaps.c是我的main是,我也确实有以下内容:
#include "./cminpack/minpack.h"
#include "./greeter.h"
  • enterer.c是另一个文件,其中

包含swaps.c我调用函数来自forter.c和函数hybrd _来自cminpack/hybrd.c。我想将同时enterercminpack链接到我的swaps

我设法通过运行以下GCC命令来链接entry

sudo gcc -c swaps.c enterer.c; sudo gcc -o swaps swaps swaps swaps swaps swaps.c engreter.c -lm; ./交换

现在,如果我尝试链接cminpack我正在运行以下GCC命令:

sudo gcc -c swaps.c enter.c -i.c -i./cminpack; sudo gcc -o交换swaps.c enterer.c -lm -l /cminpack < /code>

这会导致臭名昭著的链接错误:

/usr/bin/ld: /tmp/ccxwxt6A.o: in function `fsolve':
swaps.c:(.text+0xfb3): undefined reference to `hybrd_'
collect2: error: ld returned 1 exit status

如何链接子文件夹?

(GCC版本10.2.1 20210110(Debian 10.2.1-6))

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 can make it successfully
  • swaps.c is where my main 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))

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉南桥 2025-02-06 01:43:27

-l cminpack外,还应将-lminpack添加到gcc命令行。

应该没有理由使用sudo进行此程序的汇编,如果您无法直接调用gcc,则您的系统被错误配置并应修复。将编译器作为根而高度灰心。

使用以下行中的主目录中创建makefile

%.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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文