This is a discussion on Dumb Newbie problem -- can't compile within the Coding in General forums, part of the Development/Scripting category; Hi, I am trying to teach myself C++ and to start I plan to learn C. I have recently set ...
|
|||||||
| Register | FAQ | Members List | Calendar | Forgotten your password? | Mark Forums Read |
|
|||
|
Hi,
I am trying to teach myself C++ and to start I plan to learn C. I have recently set up a Red Hat FC4 box from a commercial DVD without any problems. I have bought a couple of C and C++ books and wrote a very simple program to test the compiling process. The C code is as follows: #include <stdio.h> main() { printf("\nHello World\n"); } Using the command: "gcc testprog.c -o testprog.out" it compiles without error messages and produces the output file (which shows as an executable file in the file browser). My problem is that when I type "testprog.out" at the command prompt I get the following response: bash: testprog.out : command not found What am I doing wrong? In frustration after much experimentation I changed the code to be C++ compatible: #include <iostream> using namespace std; int main() { cout<<"Hello World\n"; } This does not compile successfuly and gives me several errors relating to undefined references to std:cout,std::basic_ostream<<char,std::char_traits <<char and many other references to what I assume are I/O functions The error messages end with "ld returned 1 exit status" I assume this is all telling me that I have a linking problem to <iostream>. none of the books I have or the man pages give me any suggestions how to get things working. HELP!!!! |
| Sponsored Links | ||
|
|
|
|||
|
Well I have tried running the C variation with "/testprog" (and the working directory contains the executable file) but it still does not recognize the file. Adding the starting "/" changes the error message from "Command not found" to "No such file or directory"
The C++ varient still does not compile. It still seems to be a problem with using <iostream> It seems to have a problem with "std::cout" if I leave off the "std::" on cout, it just doesnt recognize it and yhe error message is much simpler (see second listing below) When I type the code as suggested: ************************************************** **** #include <iostream> int main() { std::cout<<"Hello World" <<std::endl; return 0; } ************************************************** *** I get the following when I try to compile it: [hgerar@localhost cdev]$ gcc testprog2.C -o testprog2 /tmp/ccQqAE2Y.o(.text+0x25): In function `main': testprog2.C: undefined reference to `std::cout' /tmp/ccQqAE2Y.o(.text+0x2a):testprog2.C: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std: /tmp/ccQqAE2Y.o(.text+0x35):testprog2.C: undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' /tmp/ccQqAE2Y.o(.text+0x3b):testprog2.C: undefined reference to `std::basic_ostream<char, std::char_traits<char> >: /tmp/ccQqAE2Y.o(.text+0x59): In function `__tcf_0': testprog2.C: undefined reference to `std::ios_base::Init::~Init()' /tmp/ccQqAE2Y.o(.text+0x86): In function `__static_initialization_and_destruction_0(int, int)': testprog2.C: undefined reference to `std::ios_base::Init::Init()' /tmp/ccQqAE2Y.o(.eh_frame+0x11): undefined reference to `__gxx_personality_v0' collect2: ld returned 1 exit status If I leave out the "srd::" infront of cout I get the following: ********************************************** #include <iostream> int main() { cout<<"Hello World" <<std::endl; return 0; } *********************************************** I get the following: [hgerar@localhost cdev]$ gcc testprog2.C -o testprog2 testprog2.C: In function ‘int main()’: testprog2.C:5: error: ‘cout’ was not declared in this scope If I put in the "using namespace std;" I get the same rror message as with "std::cout" I am confused!!!?? Why can't it find "std::cout"? |
|
|||
|
Quote:
./myprog.out |
|
||||
|
Henry,
You are using which Linux distro? You need to have full development tools installed as well as all libraries. For c prog Code:
gcc myprog.c Code:
./a.out
__________________
Rocky Jr. You may have my body & soul, but you will never touch my pride! If you have knowledge, let others light their candles at it. Certified to work on HP-UX / Sun Solaris / RedHat |
|
|||
|
OK.
I can get my c programs to run using the "./" in front of the file name! As far as the distro I am running, it is a recent Redhat FC4 from the commercial DVD that came with the "Red Hat Fedora and Enterprise Linux 4 Bible" book. I chose the install all packages option, so all development tools and libraries should be installed. The version of GCC is 4.0.1 |
|
|||
|
:P Well I figured out how to make my X++ code compile!!
It was obviously some sort of library linking issue. I had used the command gcc testprog2.cpp -o testprog2 without success. I think that library linking options on this command would make it work, but I found a reference in the GCC info pages that mentioned that using the G++ command automatically included the C++ libraries in the compile process. So I tried it and it works perfectly! g++ testprog2.cpp -o testprog2 Ok so now I can at least compile the code, now I just have to learn the language..... |
![]() |
| Bookmarks |
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) |
|
| Thread Tools | |
| Display Modes | |
|
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Newbie here...looking to install | Newlnx | Getting started tutorials | 4 | 03-25-2008 12:26 AM |
| sa-compile | diptanjan | Linux software | 1 | 06-07-2007 08:07 PM |
| squid for newbie | raghuram | Linux software | 5 | 02-17-2007 05:47 PM |
| Kernel Compile | mannrj45 | Linux software | 1 | 12-02-2006 09:32 PM |
| problems starting squid for a rank newbie | scarletlancer | Linux software | 1 | 11-02-2006 05:20 PM |