Thread: first program in *nix

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    163

    first program in *nix

    Edit: Ok, so I should have been using g++ instead of gcc for the .cpp file. That's settled. New question though, how do I link files that I have included? Say if I write a program and I want to include curses.h, how do I compile that?

    Everything below this is the original question.



    Hi all. Up until this point I've always written and compiled my programs in Windows. I got a Macbook Pro a couple of months ago, and figure it's time to start programming in Unix. I'm not very advanced in terminal, so it took me a bit of time to try to even get to the proper directory. I finally got the commands correct to compile but I got errors on everything, including a hello world program!


    Every time I try to compile something I get this:
    Code:
    /usr/bin/ld: Undefined symbols:
    And a bunch of stuff depending on what I'm trying to compile

    for
    Code:
    #include <iostream>
    using namespace std;
    
    int main( int argc, char *argv[] )
    {
        cout << "Hello World" << endl;
        return 0;
    }
    I get this in the terminal:

    Code:
    current directory$ gcc -o helloworld helloworld.cpp
    /usr/bin/ld: Undefined symbols:
    std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))
    std::ios_base::Init::Init()
    std::ios_base::Init::~Init()
    std::cout
    std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)
    std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)
    ___gxx_personality_v0
    collect2: ld returned 1 exit status
    I have no idea what most of this means. Is there a reason why such a simple program won't compile? I know it's probably something stupidly simple, but as I've never worked in this environment before, it's totally beyond me.

    If anybody can offer some advice, or show me a good starters guide to writing/compiling c/c++ in *nix, that be great.

    Thanks so much.

    Ed
    Last edited by System_159; 09-12-2007 at 04:09 PM. Reason: answered previous question, new question added

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    g++ prog.cpp -lcurses
    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.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    163
    Quote Originally Posted by Salem View Post
    g++ prog.cpp -lcurses
    Thanks. That solved the curses.h linker error. What do I for custom header files? I've got three header files that I'm including:

    Code:
    #include "includes/mnemonics.h" 
    #include "gui/draw_menu.h"
    #include "gui/menus.h"
    Thanks again for the help Salem. I'm currently fiddling around with the system while I wait to find out how to link those headers.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Code:
    g++ -I../includes../gui -o helloworld helloworld.cpp -lcurses
    ssharish
    Last edited by ssharish2005; 09-12-2007 at 06:55 PM.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Do nothing with the custom header files, leave them as they are, don't attempt to compile them seperatly (ie just leave the compiler switches as they are).

    You may also want to turn up the warning level and stuff, do a man gcc

    You don't need to specify -I, because you explicitly define the directory where the headers are.
    Quote Originally Posted by man gcc
    -I dir
    Add the directory dir to the list of directories to be
    searched for header files. Directories named by -I are
    searched before the standard system include directories.
    If the directory dir is a standard system include
    directory, the option is ignored to ensure that the
    default search order for system directories and the
    special treatment of system headers are not defeated .
    Last edited by zacs7; 09-12-2007 at 10:17 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 01:38 PM
  2. Need help with a program, theres something in it for you
    By engstudent363 in forum C Programming
    Replies: 1
    Last Post: 02-29-2008, 01:41 PM
  3. Replies: 4
    Last Post: 02-21-2008, 10:39 AM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM