GCC/C++无法链接库
我只是 C++ 的初学者。我正在尝试构建一些头文件 header.h,但输出总是如下所示:
/tmp/ccTmZKXX.o: In function `main':
main.c:(.text+0x13): undefined reference to `func'
collect2: ld returned 1 exit status
你能帮我看看我使用头文件的方式是否正确?多谢!
主代码(main.c
):
#include "stdio.h"
#include "func.h"
main() {
double a = f(2.3);
printf("a=%f\n", a);
}
其中func.c
包含:
double func (double x) { return x ;}
其中func.h
包含:
double func (double);
并且我编译:
gcc -o main main.c
I am just a beginner in C++. I am trying to construct some header file header.h, but the output is always like the following:
/tmp/ccTmZKXX.o: In function `main':
main.c:(.text+0x13): undefined reference to `func'
collect2: ld returned 1 exit status
Could you please help me to see whether my way of using header file is correct or not? Thanks a lot!
Main code (main.c
):
#include "stdio.h"
#include "func.h"
main() {
double a = f(2.3);
printf("a=%f\n", a);
}
where func.c
contains:
double func (double x) { return x ;}
where func.h
contains:
double func (double);
And I compile with:
gcc -o main main.c
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这里存在多个问题:
GCC(GNU编译器集合)中的C++编译器是
g++
,而不是gcc
;后者是GNU C 编译器。main.c
中的代码是一个(不是很好的)C 程序,而不是 C++ 程序。 C99 禁止隐式int
返回类型; C++ 本质上从来不允许这样做。你的问题使用了函数
f
;您的编译错误引用了func
。这意味着您没有准确地向我们展示您尝试编译的代码。标准规定
#include
;你也应该这样做。注意:如果您使用 C99,这是一个非常好的 C 程序。在 C89 中,您应该从
main()
返回一个值,而不是“从末尾掉落”。 C99 遵循 C++98,并允许从末尾脱落,相当于显式return 0;
。无论如何,我倾向于放置显式的return(0);
(通常带括号,有时不带括号 - 编译器不介意任何一种方式)。 (我用-Wstrict-prototypes
编译 C;为了获得无警告编译,我编写int main(void)
,它也适用于 C++,但void
在那里不是必需的。)标题是好的,尽管你会在适当的时候了解标头防护和其他使标头更可靠的用具。
<前><代码>#ifndef FUNC_H_INCLUDED
#定义FUNC_H_INCLUDED
外部双功能(双a);
#endif /* FUNC_H_INCLUDED */
extern
不是强制性的。我倾向于使用它,但有很多人不使用它。定义函数的源文件应包含头文件,以确保函数定义与声明一致。使用该函数的所有代码都应包含标头,以便在范围内有一个原型。这种交叉检查对于可靠性至关重要。 C++ 在使用函数之前需要作用域内的原型;在定义函数之前,它不需要范围内的原型(但这样做是一个很好的做法)。在 C 中,强烈建议您在定义外部(非静态)函数之前在作用域内拥有原型。您可以将
-Wmissing-prototypes
与 C 代码和 GCC 一起使用来发现此类问题,但该选项对 G++ 无效。<前><代码>#include“func.h”
双函数(双x){返回x; }
由于这是一个 C++ 问题,我们可以考虑在标头中内联该函数。事实上,C99 也支持内联函数。不过,我们可以暂时忽略它。
由于这是一个 C++ 问题,我们可以认为使用
不好,因为它不是类型安全的。您可能最好使用
等,尤其是因为它们是类型安全的。正确的编译需要主程序及其调用的函数,因此您可以编写:
或者,如果您用 C 语言编译它,则:
请注意,
-std=c99
对于确保main()
中缺少return
是可以接受的。请注意,C++ 源代码使用了多种扩展名,包括
.C
、.cpp
和.cxx
,所有这些扩展名被 G++ 接受(以及.c
)。There are multiple problems here:
The C++ compiler in the GCC (GNU Compiler Collection) is
g++
, notgcc
; the latter is the GNU C Compiler.The code in
main.c
is a (not very good) C program and not a C++ program. C99 outlawed the implicitint
return type; C++ essentially never allowed it.Your question uses a function
f
; your compilation error referencesfunc
. This means you did not show us exactly the code you tried to compile.The standards say
#include <stdio.h>
; you should too.NB: This is a perfectly good C program if you work with C99. In C89, you are expected to return a value from
main()
rather than 'fall off the end'. C99 follows C++98 and allows falling off the end as equivalent to an explicitreturn 0;
. I tend to put the explicitreturn(0);
(usually with, sometimes without, the parentheses - the compilers don't mind either way) anyway. (I compile C with-Wstrict-prototypes
; to get a warning-free compilation, I writeint main(void)
, which also works with C++ but thevoid
is not necessary there.)The header is OK, though you will learn in due course about header guards and other paraphernalia that make headers more reliable.
The
extern
is not mandatory. I tend to use it, but there are many who do not.The source file defining the function should include the header to ensure that the function definition is consistent with the declaration. All code that uses the function should include the header so that there is a prototype in scope. This cross-checking is crucial for reliability. C++ requires prototypes in scope before a function is used; it does not demand a prototype in scope before the function is defined (but it is good practice to do so). It is strongly recommended in C that you have a prototype in scope before defining an external (non-static) function. You can use
-Wmissing-prototypes
with C code and GCC to spot such problems, but the option is not valid for G++.Since this is a C++ question, we could consider inlining the function in the header. Indeed, C99 also supports
inline
functions. However, we can ignore that for the time being.Since this is a C++ question, we could consider that using
<stdio.h>
is not good because it is not type safe. You might be better off using<iostream>
et al, not least because they are type safe.The correct compilation requires both the main program and the function it invokes, so you might write:
Or, if you are compiling it in C, then:
Note that the
-std=c99
is necessary to ensure that the absence ofreturn
inmain()
is acceptable.Note that there are several extensions in use for C++ source code, including
.C
,.cpp
and.cxx
, all of which are accepted by G++ (as well as.c
).这里有几个问题。
在func.h中定义函数如下
编译时,提供所有源(c、cpp)文件
您应该可以开始了。
There are several things wrong here.
Define the function as follows in func.h
When compiling, provide all source (c, cpp) files
You should be good to go.
像这样编译:
然后就可以了。
Compile like this:
Then it will be fine.