使用makefiles c编译函数的未定义引用

发布于 2025-01-19 19:41:22 字数 1542 浏览 3 评论 0原文

我正在尝试使用名为 coco.c 的文件中的一组函数来运行一些实验(来自 coco 优化平台,可以在 https://github.com/numbbo/coco)。该文件还附带一个头文件coco.h。我一直在尝试通过编写我自己的名为 testing.c 的文件来测试 coco.c 文件中的调用函数是否有效。

完整的testing.c 文件如下所示,

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "coco.h"

#define max(a,b) ((a) > (b) ? (a) : (b))


int main(void){
 coco_suite_t *suite;
 suite = coco_suite("bbob-biobj", "", "");
 return 0;
}

我已将 coco.hcoco.c 放在与 testing.c 相同的文件夹中> 以及我的 Makefile.incoco_suitecoco.c 中的一个函数。

我一直在使用的 makefile 如下所示(其中大部分是我从示例 makefile 中复制的)。

## Makefile

LDFLAGS += -lm
CCFLAGS ?= -g -ggdb --std+c89 -pedantic -Wall -Wextra -Wstrict-prototypes -Wshadow -Wno-sign-compare -Wconversion


all: testing

clean:
    rm -f coco.o
    rm -f testing.o testing


testing: testing.o coco.o
    gcc ${CCFLAGS} -o testing coco.o testing.o ${LDFLAGS}

coco.o: coco.h coco.c
    gcc -c ${CCFLAGS} -o coco.o coco.c

testing.o: coco.h coco.c testing.c
    gcc -c ${CCFLAGS} -o testing.o testing.c

但是,当尝试构建可执行文件时,我收到一条错误消息,指出未定义对 coco_suite 的引用。我知道函数原型位于头文件中,完整代码位于源文件中,因为我已经成功运行了 coco 平台作者提供的示例。我已经仔细检查了空格(制表符与空格等),它与提供的工作示例完全匹配。然而,即使我将示例代码复制并粘贴到 testing.c 文件中,我也会遇到相同的错误。

这让我相信问题出在 makefile 中,但我看不到它是什么。

有谁知道问题可能是什么?

I am trying to run some experiments using a set of functions inside a file called coco.c (from the coco optimisation platform which can be found at https://github.com/numbbo/coco). This file also comes with a header file coco.h. I have been trying to test if the calling functions from the coco.c file works by writing my own file called testing.c.

The full testing.c file can be seen below

#include <math.h>
#include <stdlib.h>
#include <stdio.h>

#include "coco.h"

#define max(a,b) ((a) > (b) ? (a) : (b))


int main(void){
 coco_suite_t *suite;
 suite = coco_suite("bbob-biobj", "", "");
 return 0;
}

I have placed placed both coco.h and coco.c in the same folder as testing.c along with my Makefile.in. coco_suite is a function inside coco.c.

The makefile that I have been using can be seen below (most of which I have copied from the example makefile).

## Makefile

LDFLAGS += -lm
CCFLAGS ?= -g -ggdb --std+c89 -pedantic -Wall -Wextra -Wstrict-prototypes -Wshadow -Wno-sign-compare -Wconversion


all: testing

clean:
    rm -f coco.o
    rm -f testing.o testing


testing: testing.o coco.o
    gcc ${CCFLAGS} -o testing coco.o testing.o ${LDFLAGS}

coco.o: coco.h coco.c
    gcc -c ${CCFLAGS} -o coco.o coco.c

testing.o: coco.h coco.c testing.c
    gcc -c ${CCFLAGS} -o testing.o testing.c

However, when attempting to build the executable I get an error saying undefined reference to coco_suite. I know that the function prototype is in the header file and the full code is in the source file as I have successfully run the example provided by the authors of the coco platform. I have doublechecked the whitespace (tabs vs spaces etc) and it matches exactly with the working example provided. However even when I copy and paste the example code into the testing.c file I get the same error.

This leads me to believe that the issue is in the makefile but I cant see what it is.

Does anyone know what the issue could be?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文