LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Compiling a simple C++ program (https://www.linuxquestions.org/questions/programming-9/compiling-a-simple-c-program-70975/)

Tinkster 07-09-2003 08:47 PM

man gcc

-L = Path to libraries

I find it a bit odd that you have to point
the compiler to the includes, in the first
place, though, and believe there's some-
thing wrong with your installation.

Cheers,
Tink

kbeaver 07-10-2003 03:15 PM

I found an option that works.
gcc file.cpp -lstdc++

What does that mean?

Tinkster 07-10-2003 03:18 PM

That means that you're using the C compiler
instead of the C++ one and tell it to link against
the stdc++ library :}

Cheers,
Tink

captainstorm 07-11-2003 02:35 AM

?? Sorry but I am a little confused here. Do you mean you use the gcc to compile the code containing "cout<<....<<...."?

This error is also weird to me, since the code compiles well on my computer. I think probably you accidentally modified some settings of the system library or include file. like /etc/ld.so.conf

Qu Chen

kbeaver 07-11-2003 09:17 AM

I just installed Linux a week ago. I have not modified anything really. I just looked at that file, they are not where ALL of my libraries are. Should I add them?

kbeaver 07-11-2003 09:24 AM

THAT WAS IT! After I added where the includes lived (/usr/..../g++) the file compiled with out the -lstdc++ option (that is really lazy of me! I spend two days looking for a way not to type in eight characters every time I compile :) )

Thanks for all of your help guys

TheLinuxDuck 07-11-2003 09:49 AM

Quote:

Originally posted by kbeaver
I spend two days looking for a way not to type in eight characters every time I compile
Actually, I think that is one of the mission statements in the programmers handbook. (=

Mechanic 07-11-2003 07:26 PM

I'm having a similar problem but not having any luck. I'm trying to compile the "helloworld.cpp" program and getting the following error:

helloworld.cpp: In function `int main()':
helloworld.cpp:5: `cout' undeclared (first use this function)
helloworld.cpp:5: (Each undeclared identifier is reported only once for each function it appears in.)
helloworld.cpp:5: `endl' undeclared (first use this function)

The code is:

#include <iostream>

int main()
{
cout<<"Hello World!"<<endl;
return 0;
}

I tried the solutions that you guys tried (including 'gcc file.cpp -lstdc++) but they didn't work. I think my C++ libraries are in /usr/include/c++/3.2. The iostream lib is there. Any thoughts? Thanks.

kev82 07-11-2003 07:31 PM

either prefix cout and endl with std:: or add the line

using namespace std;

before main

Mechanic 07-11-2003 11:23 PM

Thanks Kev! The 'using namespace std;' worked. But tell me, why do you have to use that? Why is it not enough to use g++ and c++ libraries? Thanks again.

Tinkster 07-12-2003 01:30 AM

Because namespaces make sense,
and the new standard requires either
adding the namespace to the function,
or the "using namespace std" at the
beginning. The latter (even though more
convenient) is considered worse style.

Cheers,
Tink

verdeboy2k 02-06-2004 07:35 PM

EDIT Will someone who can plz delete this, silly me didn't see the part about prefixing std:: before cout, etc. That fixed it. But still, why is the std:: part necessary, i have used gcc before and it didn't require this. /EDIT

i have had the same problem, i use g++ 3.2.3. the headers are
Code:

#include <iostream>

using namespace std;

BUT it still spouts forth about 30 error messages that even i can't understand.

lyle_s 02-06-2004 08:02 PM

Quote:

Originally posted by kbeaver

: undefined reference to `endl(ostream&)'

I looks like you're using the "gcc" command to compile the program. You've got to use the "g++" command to compile a C++ program.

Lyle


All times are GMT -5. The time now is 06:23 PM.