🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

ld and cc/gcc help

Started by
4 comments, last by manCode 23 years, 2 months ago
alright, this is going to be a real newbie question, but, I have just started to program under Linux and command-line UNIX. Now, I can it to compile my file (hello.cc - basic helloworld file) and have the object file of hello.o . Now, either I''m using cc/gcc wrong (cc -o hello hello.cc will attempt but will have object file errors, so I try cc -o hello -c hello.cc to stop after compiling) or the ''ld'' command (ld -o hello hello.o). Either way, it will not link correctly, and I need some help. If you could give me any information, or even just where to find the information, that would be very appreciative. Thank you again. PS: Perhaps it would be simpler to just tell me how to compile/link a file under command-line UNIX. Thanks! "Just remember, it''''s not the fall that kills you, it''''s the sudden stop."
"Just remember, it''s not the fall that kills you, it''s the sudden stop."
Advertisement
post your source code and/or errors.
You''re probably getting something like this:

  gcc helloworld.cpp/tmp/ccm6EbgN.o: In function `main'':/tmp/ccm6EbgN.o(.text+0xa): undefined reference to `endl(ostream &)''/tmp/ccm6EbgN.o(.text+0x17): undefined reference to `cout''/tmp/ccm6EbgN.o(.text+0x1c): undefined reference to `ostream::operator<<(char const *)''/tmp/ccm6EbgN.o(.text+0x2a): undefined reference to `ostream::operator<<(ostream &(*)(ostream &))''collect2: ld returned 1 exit status  


Here''s what you need to do

1. Find out where libstdc++.so is located. If there is more than one location pick either one.
2. Compile your c++ code

gcc -L/path/to/the/directory/containing/libstdc++ -lstdc++ main.cpp

The -L means to add this directory path to the list of paths that the linker will use to look for libraries specified with the -l option.
The -l option adds the specific library (the real library''s name is libNAMEOFLIB.so You just use the NAMEOFLIB in the -l option).

The above statement compiled and linked okay with the gcc compiler distributed with RedHat 7.0 You should find the resulting exectuable as a.out in the directory where compilation was initiated.

joeG

joeG
Hi,
Thats what you get if you compile c++ files with a c compiler

Change gcc to g++.
g++ helloworld.cpp

Hello from my world
While it''s definitely good to learn basic Unix commands, I urge you to use autoconf/automake for your bigger projects. While it seems horrible to use at first, it''s actually not that hard to set up once you know how to do it.

The basic step in creating such a project are:

1. Creating a simple configure.in in your directory, something like that:

dnl Process this file with autoconf to produce a configure script.AC_INIT(configure.in)dnl Project name and version numberAM_INIT_AUTOMAKE(yourproject,0.01)dnl Optional: this creates a config.h which can hold additional stuffdnl AM_CONFIG_HEADER(config.h)dnl Checks for programs (C compiler; check info autoconf for more)AC_PROG_CCdnl Checks for libraries.dnl Checks for header files.dnl Checks for typedefs, structures, and compiler characteristics.dnl Checks for library functions.AC_OUTPUT( \./Makefile) 


2. Create a basic Makefile.am like this:

bin_PROGRAMS	= yourprogyourprog_SOURCES	= main.c yourprog_LDFLAGS	= -lcryptAM_CFLAGS = -Wall 


The above will give you the highest level of gcc warning messages, which can be _very_ useful. It also links with the encryption library (which you''ll probably not need )

3. Run the following programs on the console:

autoheader (run this only if you choose to use a config.h)
aclocal
autoconf
automake -a -c

The options -a -c will make automake copy missing files into the directory. It''ll still bitch about missing README etc...
To stop the bitching, just add empty files for the "missing" ones.

4. Just build your project like you build most Linux packages from source:

./configure && make

(you won''t want make install just yet )

The nice thing about autoconf/make is that it takes care of all the typical things like CFLAGS for you, and, what''s even better, it does all the dependencies (including header files) automatically for you...

cu,
Prefect

Resist Windows XP''s Invasive Production Activation Technology!
One line of sourcecode says more than a thousand words.
Widelands - laid back, free software strategy
Hey guys, thanks. I think that I finally figured out that I should be using g++ instead of gcc and am going to go try the other couple of methods (except maybe autoconf and automake (i just now got ''make'' to work!)). I just wanted to say thanks for all of you help again. You guys treat newbies real well. I hope that I can return the favor eventually. Thanks again!

"Just remember, it''''s not the fall that kills you, it''''s the sudden stop."
"Just remember, it''s not the fall that kills you, it''s the sudden stop."

This topic is closed to new replies.

Advertisement