g++: undefined reference

1
2
3
4
5
6
       //main.cpp
      int puts ( const char * str );
      int main()
      {
      puts("Hello world\n");
      }


HI guys,
The above won't work in g++. But some one i know says it works in other compilers.
puts() is a function in C standard library.
So is there any option i have to use in order to make it work?
Thank you very much.
Last edited on
If it is in the C std library, then you shouldn't be declaring your own prototype. Considering there are no multiple definition errors, I think you are not including <stdio.h> (for C) or <cstdio> (for C++).
Last edited on
Thank you for your help. But the requirement is that i can not use any header file. This is the problem of Thinking in C++ chapter 10 question 35.
Ok, IMHO that's a stupid assignment.

1
2
3
namespace std {
    int puts( const char* );
}


the problem is:
"Without including a header file, declare the function puts() from the Standard C Library. Call this function from main()."

I write:
1
2
3
4
5
6
7
namespace std{
  int puts(const char*);
}
main(){
  using namespace std;
  puts("h");
}


Then I got error:
In function `main': undefined reference to `std::puts(char const*)'
Why is that?
Thank you.
You are not linking against the C++ runtime library for some reason. Don't know why your compiler is not including it by default. The code is otherwise correct and compiles and runs fine for me.
Topic archived. No new replies allowed.