ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Introduction to Linux - A Hands on Guide
This guide was created as an overview of the Linux Operating System, geared toward new users as an exploration tour and getting started guide, with exercises at the end of each chapter.
For more advanced trainees it can be a desktop reference, and a collection of the base knowledge needed to proceed with system and network administration. This book contains many real life examples derived from the author's experience as a Linux system and network administrator, trainer and consultant. They hope these examples will help you to get a better understanding of the Linux system and that you feel encouraged to try out things on your own.
Click Here to receive this Complete Guide absolutely free.
Ok so bare with me please as im quiet nex to c++ and linux. I borwed a book to help me learn C++ the book starts out with an example program to get you started it is:
//write to the screen
std:cout << "My first c++ program";
so i saved the file as frst.c and ran it in gcc. I recieved the following error:
first.c:1:20: iostream: No such file or directory
first.c: In function `main':
first.c:6: error: `cout' undeclared (first use in this function)
first.c:6: error: (Each undeclared identifier is reported only once
first.c:6: error: for each function it appears in.)
My question inst so much that I got an error, but rather is it possbile to have c++ examples that only work in windows? As i thought c++ was not platform dependent.
So I then found the example c++ program called hellow
return 0; /* Just to be nice */
Now this one worked with no other issue than
hello.c:7:2: warning: no newline at end of file
this one produced the output file a.out and I was able to run it and receive the desired output message from the program in console.
So maybe someone could let me know what is wrong with the above program? i just want to nkow before I start getting into this book and start having more problems running there example programs.
Sorry if this doesnt make alot of sence, i tried to state it the best way i could.
Thank you again to anyone who may help or guide me in this issue.
Hey thanks for the response dizzutch and Dodgeram01.
Ok so I did somemore reading and yes the top one is C++ the other is C, so since im trying to learn C++ ill stick with the first one mentioned above. So I renamed the file first.cpp from first.c
I then ran at console g++ first.cpp ( I ran --help first but wasnt really sure what to use since im a new at this so just ran g++ first.cpp)
And I recieved the same above errors
first.cpp: In function `int main()':
first.cpp:6: error: use of namespace `std' as expression
first.cpp:6: error: syntax error before `:' token
So im not sure if this is somethign I am doing wrong or am I missing some sort of library?
But thanks for your help Im willing to try anything to learn what it is im doiing wrong.
Ok so i found my first mistake:
in the code i typed
should have been std::cout
So it worked in g++ it created the a.out file. But a friend of mine ran his with
and his worked?
Mine when ran through gcc gives me this:
/tmp/ccXsPy2h.o(.text+0x25): In function `main':
: undefined reference to `std::cout'
/tmp/ccXsPy2h.o(.text+0x2a): In function `main':
: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/tmp/ccXsPy2h.o(.text+0x56): In function `__static_initialization_and_destruction_0(int, int)':
: undefined reference to `std::ios_base::Init::Init[in-charge]()'
/tmp/ccXsPy2h.o(.text+0x85): In function `__tcf_0':
: undefined reference to `std::ios_base::Init::~Init [in-charge]()'
/tmp/ccXsPy2h.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
So i guess I am happy that it worked and created the ou
tput in g++ but still a bit perplexed as why i cant do it in gcc and a friend can? we both are running suse9.2 with all the current updates.
so anymore help is grealtly welcomed.
Both gcc and g++ are front-ends for the GNU compiler collection. AFAIK g++ just sets some options in terms of lnking in the C++ standard libraries. I think if you name your program file (i.e. with a C++ file extension such as .C or .cxx, gcc will pick up on the fact that it's C++ and linkthose in too. This is probably what your friend did. If you try to compile and link C++ without linking in the C++ standard libs, as you saw, bad things can happen.
What version of gcc do you have? I think more recent ones willrecognize cpp as a valid C++ extension.
[Edit: I just tried this (I don't do C++ very much) and I couldn't make it work without using g++ -- are you sure your friend was able to? maybe he explicitly linked inthe C++ libs.]
I said it wasn't a good idea to use it in production code, that isn't to say programmers (professional or not) do it anyway.
The reason is, you're effectively importing the entire namespace. It defeats the purpose of having the namespace for organization. You may run into an error down the line with a function name clashing with something in std. The best thing to do is to either specify the full name (scope included) of the function.
Aside from function name clashing, there's the possibility of unnecessary overhead. This isn't always the case, but, it's something to consider. In my opinion, leaving the namespace std alone often makes code look better too. "Where is that function? Oh, it's in std."
Thanks again for all the input into this question!!
Well if you noticed in my first post above I did originally use the .C for my file extension. ANd still could not get it to work in gcc (gcc version: 3.3.4)
And I know the code may not be written "perfectly" but that source was taken line from line from a book that I am trying to use to teach myself with. So I was a bit foggy as to why it wouldnt work with gcc and would with g++.
So as you can see I really dont know much about c++, basically I was just reading through my book "Wiley's Tech yourself C++ 7th Edition" (got this book used for dirt cheap so I thought it would be a good score) and saw that first snippet of code and thought I would try it just to see if it would work.
I have seen some books that are specifically titled "c++ for Linux" and such, but I thought since c++ wasnt platform dependent that I could use whatever book to help teach me the foundations of programming in C++. Am I wrong in this statement? Or better yet is there a book or books someone can recommend for learning c++?
Well thanks again for all your help here, I still have lots more to learn so I sure I will be back to pick the minds of the linuxquestions community!!
Thanks again, help is very much appreciated over here
The C/C++ language itself isn't platform dependent, but there are platform specific APIs... For instance, in Windows you have the Win32 API, which is specific to Windows. For Linux/UNIX you have a number of other APIs that are specific to UNIX based platforms.
However, even though those APIs are fairly platform specific, you can sometimes get a port of them. For instance, I believe that Winelib is basically a port of the Win32 API functions to Linux, and for Windows, you can get most of the Linux/UNIX APIs using a tool like Cygwin...
If you stick to those functions of C/C++ that are defined by the ANSI/ISO standard, your code should stay pretty portable. Once you are ready to move on to other APIs, you should better understand C/C++ and be able to make choices of the APIs you want to use.