Problems with c++

Elius2

New member
Problems with c++

I'm trying to start learning cpp, and I'm stuck on the first hello world program. #include <iostream.h> int main() { cout<<"HEY, you, I'm alive! Oh, and Hello World!"; return 0; } When I'm trying to compile it using GCC I'm getting errors. ======Begin Error======== elius@buga cpp $ gcc 1.cpp -o 1.c In file included from /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/include/g++-v3/backward/iostream.h:31, from 1.cpp:1: /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.1/include/g++-v3/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated. /tmp/cceborwk.o(.text+0x1b): In function `main': : undefined reference to `std::cout' /tmp/cceborwk.o(.text+0x20): In function `main': : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /tmp/cceborwk.o(.text+0x49): In function `__static_initialization_and_destruction_0(int, int)': : undefined reference to `std::ios_base::Init::Init[in-charge]()' /tmp/cceborwk.o(.text+0x7a): In function `__tcf_0': : undefined reference to `std::ios_base::Init::~Init [in-charge]()' /tmp/cceborwk.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status =======End Error======== What is the problem ? how to modify it to work with gcc?​
 

eyalbd

New member
אין הודעת שגיאה טובה יותר מזו...

שים לב להודעה של הקומפיילר:
This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section 17.4.1.2 of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>.​
שים לב שהוא מדריך אותך כאן להשתמש ב:
#include <iostream> ולא ב #Iinclude <iostream.h>​
בנוסף, כל השמות של הספריה הסטנדרטית בתוך namespace בשם "std". זה כדי שלא יהיה ערבוב בין מזהים משלך ובין אלה של הספריה הסטנדרטית. התוכנית המתוקנת היא:
#include <iostream> int main() { std::cout<<"HEY, you, I'm alive! Oh, and Hello World!"; return 0; }​
 

Elius2

New member
Thanks you 2 but...

This is what I get when I wrote it like you said: ====My code:===== #include <iostream> int main() { std::cout<<"HEY, you, I'm alive! Oh, and Hello World!"; } ====End code====== =====Error====== elius@buga cpp $ gcc 1.cpp -o 1.out /tmp/ccrNtIh5.o(.text+0x1b): In function `main': : undefined reference to `std::cout' /tmp/ccrNtIh5.o(.text+0x20): In function `main': : undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::eek:perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' /tmp/ccrNtIh5.o(.text+0x49): In function `__static_initialization_and_destruction_0(int, int)': : undefined reference to `std::ios_base::Init::Init[in-charge]()' /tmp/ccrNtIh5.o(.text+0x7a): In function `__tcf_0': : undefined reference to `std::ios_base::Init::~Init [in-charge]()' /tmp/ccrNtIh5.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status =====End error===== I read the error but i dotn really understand it :X What does it want now ? (btw you guys code on linux/unix platforms?)​
 

עידו123456

New member
לא שמתי לב קודם

אבל אתה מנסה לקמפל עם gcc שהוא קומפיילר ל- c, תריץ את הפקודה עם ++g:
$ g++ 1.cpp -o 1.out​
או אם לא אכפת לך שהקובץ יקרא a.out:
$ g++ 1.cpp​
ה- $ הוא כמובן השורה של הshell. נ.ב. את כל התכנות שלי אני מבצע על linux, אין לי אפילו קומפיילר פשוט על ה windows...
 

Elius2

New member
Thanks works!! Silly me

What linux dist you are using ? And what editor are you using ? I saw pictures from kdevelop, looks real good.​
 

עידו123456

New member
מנדרייק 9.2 בבית

ודביאן באוניברסיטה (שה- system כמובן התקינו...) אני מתכנת עם kdevelop וממש מרוצה, מה עוד שאוטוטו תצא גרסא 3 של kdevelop (ביחד עם kde 3.2 - בתחילת דצמבר?) ב debugger אני מחליף בין הפנימי של kdevelop ובין ddd (תלוי בצורך) אפשר תמיד גם להשתמש ב emacs שעושה את העבודה נאמנה, אבל אני פשוט מעדיף את הנוחות של IDE. מה עם האנגלית? לא סידרת את התמיכה בעברית ל KDE? וכמובן שאין שום בעיות שתשאל שאלות, אם לא תשאל, איך תדע?
 

Elius2

New member
Dont use hebrew on windows either

I'm using gentoo, love it so much. Recomend you to use it also. I dont use KDE, I'm using flux box. I'm having problems installing kdevelop, emerging it doesnt work. It tries to get alpha 7 version i think which isnt stable so i'm getting an error. Now compiling a version from their web site. Another question (last 1 for the night), Know any good sites that got good tutorials for Cpp on unix/linux enviroment ?​
 

barakbl

New member
למה?

אתה כנראה עוד לא ממש מבין את העוצמה של portage (המערכת של ג'נטו לניהול החבילות במערכת). כדי להתקין גרסא יציבה ישנה יותר אתה צריך להעביר אותו ל emerge בצורה מפורשת, כלומר:
emerge kdevelop​
אמור להביא אותך לגרסא האחרונה של kdevelop. כשבעץ יכול להיות שיש גרסא שנחשבת ללא יציבה, לפיכך רק אם אתה על העץ הלא יציב היא זו שתורד ותקומפל או האחרונה היציבה במידה ואתה מוגדר לעבודה על העץ היציב. בכל מקרה, אתה יכול לנסות לקחת גרסא ישנה יותר, שים לב שכל חבילה שאתה מנסה להוריד למחשב, בשורה התחתונה, המערכת יודעת מאיפה להוריד ואיך לקמפל/להתקין וכו' על ידי קבצי סקריפטיפ, הנקראים איבלדים.אם אתה מעביר בצורה ידנית את מיקום האיבילד שאתה רוצה להתקין (יש איבילד לכל גרסא) המערכת תתקין את הגרסא הספציפית שאתה מעוניין בה, או אם ניקח דוגמא פשוטה:
emerge /usr/portage/dev-util/kdevelop/kdevelop-2.1.5.ebuild​
יוריד ויתקין לך את גרסא 2.1.5 של kdevelop .... יש שם מספר גרסאות שונות כמובן. בכל מקרה, אני ממליץ לתת צ'אנס גם ל anjuta החביב.
 

Elius2

New member
mmmm....

I did emerge to kdevelop but it only downloaded the alpha 7 version I think. And I dont know how to get older versions. Anyhows I've downloaded kdevelop latest stable version from their web site and i'm happy with it.​
 

eyalbd

New member
התוכנית gcc היא לא הקומפיילר

היא רק front-end לקומפיילרים השונים. כנ"ל גם ++g. בשני המקרים הקומפילציה זהה אבל הלינק שונה. ++g מוסיף גם את הספריות של ++C ללינקר. gcc לא מוסיף אותן וזה המקור לבעיה כאן כי זו שגיאת לינק. הקומפיילרים עצמם הם cc1 ל-C ו cc1plus ל ++C.
 

עידו123456

New member
ככה:

אל תשתמש ב <iostream.h> אלא ב <iostream>, זהו שימוש לא נכון בסיפריות. תוסיף ::std ל- cout (כלומר std::cout) ככה אתה מציין לקומפיילר ש- cout היא פקודה ששייכת לסיפריה std. אתה יכול לחילופין לשים את הפקודה ";using namespace std" לאחר הinclude ואז הפקודה תעבוד גם בלי ::std אבל אני מציע לא להתעצל ולהוסיף את ::std, זה יכול לחסוך בעיות בעתיד. אין צורך להוסיף את return 0 בסוף, הקומפיילר מטפל בזה באופן עצמאי.
 

gmorphus

New member
למה אין צורך להוסיף?

ולסמוך על הקומפיילר??? זה תכנות מאוד לא נכון ואני חולק על העצה הזו. חשוב להוסיף את הreturn 0 כדי שלך תהיה בקרה על מה שאתה כותב ועל הערכים המוחזרים.
 

עידו123456

New member
וכשאתה

מכניס סטרינג למערך אתה מכניס באופן עצמאי '0\' לסוף המערך או שאתה סומך על הקומפיילר? אני יודע שהקומפיילר שלי מטפל בזה, אז למה להסתבך? ולמה זה "תכנות מאוד לא נכון" במקרה הקיצוני שהקומפיילר לא יתמודד עם הפעולה הפשוטה הזאת, אני אוסיף return. כרגע אני מתכנת לקומפיילר ספציפי, אם אני ארצה ליצור קוד שמתאים ליותר מקומפיילר אחד, אני בכל מקרה אצטרך גם להקפיד יותר על ++c תיקני, כי לכל קומפיילר יש את השגעונות שלו.
 

Elius2

New member
Sorry for the stupid questions

I know they are probally very dumb, but I'm new to CPP,GCC and linux. I hope you wont mind help a n00b in his begging​
 
למעלה