在 GCC 中使用多个头文件进行编译——对 .. math 的 undefined reference

标签 c gcc compilation

我已经研究了一段时间了。我有这些文件:

main.c
zSim.h 
zSim.c
zDynamicArray.h 
zDynamicArray.c
zOptions.h 
zOptions.c
zMain.h 
zMain.c

所有文件和 header 都位于同一文件夹中。

我的主要有以下内容:

#include "zDynamicArray.h"
#include "zOptions.h"
#include "zMain.h"
#include "zSim.h"
#include <stdio.h>
#include <math.h>

我正在使用头部防护。例如,这是来 self 的 zSim.h 文件:

#ifndef SIM_H
#define SIM_H
#include "zDynamicArray.h"
#include "zOptions.h"

//This Header is accountable
//for all Simulation Related things. 

int Simulate(SIMOPT so,PRINTOPT po);

#endif

这是我的 zSim.c 代码的一个片段(也许我在这里做错了什么?):

#define M_PI 3.14159265358979323846
#include "zSim.h"
#include "zDynamicArray.h"
#include "zOptions.h"
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

我正在使用这个 gcc 命令进行编译:

gcc main.c zSim.c zOptions.c zDynamicArray.c zMain.c -o TEST

我还用过:

cc -c main.c
cc -c zSim.c
cc -c ... etc
cc *.o -o TEST

它们都会导致对 math.h 库中任何内容的 undefined reference 。此外,当我使用 gcc -I 命令时,数学仍然未定义。

如果我使用 gcc main.c 进行编译,我会在我的头文件中得到任何未解析的引用。

我该怎么办?

最佳答案

尝试在末尾添加 -lm :gcc -c main.c cc -c zSim.c cc -c ... etc cc *.o -o TEST -lm

您必须使用 -lm 选项链接数学库。原因在这里解释:Why do you have to link the math library in C?

关于在 GCC 中使用多个头文件进行编译——对 .. math 的 undefined reference ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15433100/

相关文章:

c - C90 中真的不允许使用可变长度数组吗?

c - .c 源文件消失了,还有希望吗?

c - 从结构指针打印

c - 我不确定应该用 scanf 输入多少个数字

c - C 中的枚举类型变量声明

c++ - 我可以忽略 gcc 警告 : ‘Foo::m_bar’ should be initialized in the member initialization list [-Weffc++]

c# - 为什么这个带有可空值的 C# 重载没有歧义?

c - 使用指针从另一个编译单元访问和更改静态变量

c - DLL 中的函数未返回正确值

c++ - 如何忽略 'comparison between signed and unsigned integer expressions' ?