e****d 发帖数: 333 | 1 进入未名形象秀
我的博客
[回复文章] [回信给作者] [本篇全文] [进入讨论区] [返回顶部] [修改文章] [删除
文章] [转寄] [转贴] [ 1 ]
发信人: everid (everid), 信区: Programming
标 题: 一道C++习题求解。
发信站: BBS 未名空间站 (Thu Feb 26 00:32:25 2009)
Without including a header file, declare the function puts() from the
Standard C Library. Call this function from main().
我写:
//main.cpp
namespace std{
int puts(const char*);
}
main(){
using namespace std;
puts("h");
}
>g++ main.cpp
报错:
In function `main': undefined reference to `std::puts(char const*)'
怎么回事呢? | t****t 发帖数: 6806 | 2 this is C function, so you must declare it as
extern "C" int puts(const char *);
alternatively, use C compiler (gcc) and change the file name to .c as
opposed to .cpp
I think the question explicitly said "standard c library". use c++ here is
画蛇添足.
【在 e****d 的大作中提到】 : 进入未名形象秀 : 我的博客 : : [回复文章] [回信给作者] [本篇全文] [进入讨论区] [返回顶部] [修改文章] [删除 : 文章] [转寄] [转贴] [ 1 ] : 发信人: everid (everid), 信区: Programming : 标 题: 一道C++习题求解。 : 发信站: BBS 未名空间站 (Thu Feb 26 00:32:25 2009) : Without including a header file, declare the function puts() from the : Standard C Library. Call this function from main().
| e****d 发帖数: 333 | 3 Thank you very much.
the problem is solved exactly as what you said.
I use gcc instead of g++, use .c instead of .cpp, use extern declaration
instead of namesapce. So in general, i switch from c++ to c and the problem
is solved.
【在 t****t 的大作中提到】 : this is C function, so you must declare it as : extern "C" int puts(const char *); : alternatively, use C compiler (gcc) and change the file name to .c as : opposed to .cpp : I think the question explicitly said "standard c library". use c++ here is : 画蛇添足.
|
|