Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

What is the proper way to compile c++ code from the terminal?

I'm using:


g++ char.gpp


And I get:


ld: library not found for -lcrt1.10.6.o

collect2: ld returned 1 exit status


So I tried setting LIBRARY_PATH to a directory that contains crt1.10.6.o, but then I get:


ld: warning: in char.gpp, file was built for unsupported file format which is not the architecture being linked (x86_64)

Undefined symbols:

"_main", referenced from:

start in crt1.10.6.o

ld: symbol(s) not found

collect2: ld returned 1 exit status


So clearly I am not picking up the right library, or perhaps just not all the libs needed. Why is this so hard? I haven't seen problems like this with earlier versions of Xcode.


I'm using the latest version of Xcode on a MacBook Pro with Lion installed.


Thanks,


Kim

Mac Pro, Mac OS X (10.6.2)

Posted on Mar 31, 2012 12:05 PM

Reply
7 replies

Apr 1, 2012 2:58 PM in response to etresoft

d'oh! I think you're right. I clearly don't know what I'm doing.


OK, so if I name the file .cc (it really IS a c++ file), the problem then becomes:


char.cc:1:20: error: iostream: No such file or directory.


So how is the compiler supposed to be instructed where to find the header files? I thought if it was properly installed, this would be set up correctly already.


BTW: the file I'm trying to compile is short:


#include <iostream>



int main()

{

char c;

std::cin >> c;

std:: cout << "the value of '" << c << "' is " << int(c) << '\n';

}


It's code straight from the Stroustrup book.


Thanks,


Kim

Apr 1, 2012 4:22 PM in response to kimhelliwell

The compilers look at the name of the file you are compiling as well as the compiler you are using. In this case, you should name your file "char.cpp" and compile it with this line:


g++ char.cpp


Then it will find the iostream header and compile with no further problem.


Also, Apple is phasing out GCC. I suggest trying the clang compiler instead. It can be very handy if you are learning because it has much better error messages. To use clang, type this:


clang++ char.cpp

What is the proper way to compile c++ code from the terminal?

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.