Unix problems

This is a discussion on Unix problems within the C++ Programming forums, part of the General Programming Boards category; Hi everone I am new to C++ programming in Unix and have been getting some strange errors. I can compile ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    12

    Unix problems

    Hi everone I am new to C++ programming in Unix and have been getting some strange errors. I can compile this code fine in visual C++ on my windows machine, but when I try to compile on a unix machine I get these errors.

    Undifined symbol
    first reference in file

    folowed by a bunch of
    "std::basic_ifstream"
    "std::.....
    "std:... etc


    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <cctype>
    
    using namespace std;
    
    int main ()
    {
    
    int wordcount = 0;
    int letters = 0;
    double average = 0;
    char ch;
    	ifstream infile;
    
    	infile.open("a:wordy.txt");
    	if (infile.fail( )) 
    	{
    	cout << "Input file opening failed.\n";
    	system("pause");
    	exit(1);
    	}
    }
    any ideas?

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    You know, it would *really* help to know what undefined symbol the compiler is telling you about. Why didn't you post the whole error message ? Anyway, this is not the solution to your problem, but your program will never work because of system("pause"). AH AH ! Caught you. You thought you could always get away using system("pause") ? Nope. Try sticking to something more standard instead of using that function. The problem is that the function is perfectly standard yet none of its arguments are. Use cin.get() instead. It's portable and does more or less the same job.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,590
    Maybe you tried to compile it with a C compiler rather than a C++ compiler.
    Or you don't have a properly installed C++ compiler.
    Paste your command line, as well as exact error messages.

    Make sure your Unix C++ compiler is installed properly by trying a C++ "hello world" program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2006
    Posts
    65
    The code compiles with some warnings and produces this output:
    Code:
    09:25 ~> g++ -Wall stupid.cpp
    stupid.cpp: In function ‘int main()’:
    stupid.cpp:11: warning: unused variable ‘wordcount’
    stupid.cpp:12: warning: unused variable ‘letters’
    stupid.cpp:13: warning: unused variable ‘average’
    stupid.cpp:14: warning: unused variable ‘ch’
    09:25 ~> ./a.out
    Input file opening failed.
    sh: pause: command not found
    09:25 ~> touch "a:wordy.txt"
    09:25 ~> ./a.out
    09:25 ~>

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    5
    hmm i will ask my roomate

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Code:
    [
    Undefined                       first referenced
     symbol                             in file
    std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned int) const/var/tmp//cc0zbedf.o
    std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<<char, std::char_traits<char> >& (*)(std::basic_osstream<char, std::char_traits<char> >&))/var/tmp//cc0zbedf.o
    std::cout                                                 /var/tmp//cc0zbedf.o
    std::ios_base::Init::Init()                     /var/tmp//cc0zbedf.o
    srd::ios_base::Init::Init()                     /var/tmp//cc0zbedf.o
    _gxx_personality_v0                         /var/tmp//cc0zbedf.o
    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic ostream <char. std::char traits <char> >& ) /var/tmp//cc0zbedf.o
    ld: fatal: Symbol referencing errors.   No output written to a.out
    collect2: ld returned 1 exit status
    sorry it took so long, cut and paste was not working either.

    the command line looks like this

    bash-3.00$ gcc workdamnit.cpp

  7. #7
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    It should be
    Code:
    g++ workdamnit.cpp
    gcc is the c-compiler.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  8. #8
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    Code:
    infile.open("a:wordy.txt");
    That isn't going to work either. Unix doesn't use drive letters.
    Try

    Code:
    infile.open("wordy.txt");
    Or

    Code:
    infile.open("/tmp/wordy.txt");

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    You'd probably want this:
    Code:
    infile.open("./wordy.txt");
    which opens wordy.txt in the current directory; or this
    Code:
    infile.open("~/wordy.txt");
    which opens wordy.txt in the user's home directory (set with the $HOME enviroment variable), which probably looks something like /home/user.

    Code:
    sh: pause: command not found
    UNIX doesn't have a pause command, either. You'd be best off with a standard way to pause the program (like getchar(); see the FAQ). Or better yet, leave it out completely, because you're running it from the command line and so don't have to pause the program to see its output.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Registered User
    Join Date
    Mar 2006
    Posts
    12
    Thanks for all the help. I got it working now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems to Make a Unix lib on a Win32 box.
    By Templario in forum Windows Programming
    Replies: 2
    Last Post: 11-21-2002, 01:22 AM
  2. UNIX book
    By Unregistered in forum Linux Programming
    Replies: 7
    Last Post: 09-05-2002, 04:38 PM
  3. Exception Handling + UNIX
    By s0ul2squeeze in forum C++ Programming
    Replies: 5
    Last Post: 01-29-2002, 07:02 PM
  4. Differences b/w Unix and Linux
    By OS Idiot in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 12-15-2001, 08:21 PM
  5. About Unix Programming - Making a career desision
    By null in forum C Programming
    Replies: 0
    Last Post: 10-14-2001, 07:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21