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

标签 c makefile

是否正在使用位于项目目录(并包含在#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/

相关文章:

c - 生产者/消费者程序出现未知错误,相信是无限循环

C 中的 Char 和 strcpy

linux - glui/usr/bin/ld : cannot find -lXmu

makefile - 使用 GNU Make 将多行变量输出到文件

macos - 在 OSX 10.10.5 上安装 OMake 时出错

c - 如何以编程方式编辑路由表

c - ## 预处理器运算符的应用和需要考虑的陷阱是什么?

检查二维数组元素的相邻元素,而无需多次遍历一个元素

java - 如何制作一个同时编译 C、C++ 和 Java 文件的 makefile

c++ - 如何构建 C++ 源代码的 "library"?