gpt4 book ai didi

c - 将另一个文件中的函数添加到项目需要修改 makefile 吗?

转载 作者:太空宇宙 更新时间:2023-11-04 04:32:16 26 4
gpt4 key购买 nike

是否正在使用位于项目目录(并包含在#include "example.h"中)的另一个文件(example.[ch])中定义的函数需要编辑项目 Makefile?
我尝试在 net-snmp 项目的一部分中使用我的函数并遇到链接器错误。错误是:

./.libs/libnetsnmpmibs.so: undefined reference to `snmpget'

nodeFunc.c 看起来像这样:

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <string.h>
#include "NodeFunc.h"
int snmpget(char* remoteip, short remoteport,char* community,int *len,char* str)
{
....using some net-snmp functions to get a certain object from a remote host....
}

我在我的主文件中使用了 snmpget 函数,如下所示:

...
snmpget(remIP,161,remComm,&lenth,ansstr);
...

最佳答案

这不是添加需要修改 makefile 的函数,而是添加源文件本身。如果在 makefile 中源文件尚未被引用为编译目标并且其目标文件不是链接依赖项,则它不会包含在构建中。

包含在构建中不是 #include 指令的目的。这仅仅包括源文件中的.h 文件的内容,以便编译器可以看到声明;它不涉及包含定义的 .c 文件 - 必须单独编译和链接,这是本例中 makefile 的作用。

关于c - 将另一个文件中的函数添加到项目需要修改 makefile 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34587772/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com