GCC newbie

GCC newbie

Post by <- Chameleon -> » Mon, 24 Feb 2003 05:30:23



I tried to compile my program source (1 file only: main.cpp) with this line

gcc -B /root/SDL-1.2.5/include main.cpp

ERROR! gcc cannot find SDL.h! (this file is in path:
/root/SDL-1.2.5/include)
WHY?  (Line in Source Code:    #include "SDL.h")

I copied this file (and sub-includes) in main.cpp path.
GOOD!
BUT now gcc has found undefined symbols!
Functions like: sin cos glVertex3i SDL_Init
This means that gcc is not linking Libraries with object from Source Code.
What I can do?

Please give me a correct gcc syntax.
I use OpenGL, GLU, <math.h>, SDL.

I cannot create Makefile! I am too newbie!

Thanks!

 
 
 

GCC newbie

Post by Dances With Crow » Mon, 24 Feb 2003 06:17:22


On Sat, 22 Feb 2003 22:30:23 +0200, <- Chameleon -> staggered into the
Black Sun and said:

Quote:> I tried to compile my program source (1 file only: main.cpp) with this
> line

> gcc -B /root/SDL-1.2.5/include main.cpp

> ERROR! gcc cannot find SDL.h! (this file is in path:
> /root/SDL-1.2.5/include)

Nope.  SDL.h should be in /usr/include/SDL/ or /usr/include/ or
/usr/local/include/ .  Are you sure you installed the library properly?
And why are you doing this as root?  The rule is, "Never do anything as
root that you can do as a normal user," and compiling programs can
certainly be done as a user.

Quote:> #include "SDL.h"
> I copied this file (and sub-includes) in main.cpp path.  BUT now gcc
> has found undefined symbols!  Functions like: sin cos glVertex3i
> SDL_Init This means that gcc is not linking Libraries with object from
> Source Code.  What I can do?

Start by finding a good introductory book on C in a Unix environment,
and pay attention to the syntax examples it gives for linking to
external libraries and finding external header files.  If you want to
link the external library FOO with your program, you've entered
'#include "FOO.h" ' in your .c file, FOO.h is in /somewhere/include ,
and your library file "libFOO.a" is in /somewhere/lib , you'd do this:

gcc -o myprogram myprogram.c -I/somewhere/include -L/somewhere/lib -lFOO

-I specifies an additional path to search for headers.  -L specifies an
additional path to search for libraries.  -l specifies the external
libraries to link against.

-B is the *wrong* option to feed gcc according to the man page.  That
option tells gcc where to find the executable, libraries, and include
files for the compiler itself.  This is typically /usr/lib/gcc and will
not be different unless you've installed the compiler to a weird place
or you are rebuilding gcc itself.

Quote:> Please give me a correct gcc syntax.  I use OpenGL, GLU, <math.h>,
> SDL.

See above.  Remember "-lm -lGL -lGLU -lGLCore -lSDL" or you'll get link
errors and be confused.  The OpenGL headers will probably be in
/usr/X11R6/include .

Quote:> I cannot create Makefile! I am too newbie!

If you're going to program in a Unix environment, you need to learn to
create Makefiles.  A good book on C in a Unix environment will give you
a basic overview; otherwise, Google for "Makefile tutorial".

--
Matt G|There is no Darkness in Eternity/But only Light too dim for us to see
Brainbench MVP for Linux Admin /
http://www.brainbench.com     /  "He is a rhythmic movement of the
-----------------------------/    penguins, is Tux." --MegaHAL

 
 
 

GCC newbie

Post by Gerhard W. Grube » Mon, 24 Feb 2003 06:36:56


On Sat, 22 Feb 2003 22:30:23 +0200 wrote "<- Chameleon ->"


Quote:>gcc -B /root/SDL-1.2.5/include main.cpp

This should read gcc -I /root/SDLblablabla main.cpp

Quote:>ERROR! gcc cannot find SDL.h! (this file is in path:
>/root/SDL-1.2.5/include)
>WHY?  (Line in Source Code:    #include "SDL.h")

You probably shouldn't do this as root, but it's your system that may be
screwed up if you do something worng. :)

Quote:>I copied this file (and sub-includes) in main.cpp path.
>GOOD!

Not good. :) Do it like above and it should work. Even better would be to
properly install SDL so that it resides in /usr/include

Quote:>BUT now gcc has found undefined symbols!
>Functions like: sin cos glVertex3i SDL_Init
>This means that gcc is not linking Libraries with object from Source Code.
>What I can do?

you need to specify the libraries from SDL and you also need to install OpenGL
headers (I don't thin they are included in SDL). If you have OpenGL installed
on your system (check with 3Ddiag) then you need also to specify the
appropriate libraries.

this would be better done with a makefile because you will probably need more
lines for compilation and you definitely dont want to specify this always on
the commandline. :)

gcc main.cpp -I <you include path for SDL> -L <librarypath> -l <library>

I'm not absolutely sure about the exact syntax but this should work IMO.

Quote:>I cannot create Makefile! I am too newbie!

Creating a Makefile would be easier. You can copy a Makefile from any working
package and then edit it.

--
Gerhard Gruber

Fr jedes menschliche Problem gibt es immer eine einfache L?sung:
Klar, einleuchtend und falsch. (Henry Louis Mencken)

 
 
 

GCC newbie

Post by Gerhard W. Grube » Mon, 24 Feb 2003 06:45:33


On Sat, 22 Feb 2003 22:30:23 +0200 wrote "<- Chameleon ->"


Quote:>I cannot create Makefile! I am too newbie!

I just looked up my own makefile that I created, which is rather simple so you
should be able to use it.

--------------------
CC = gcc

PROGRAM = Calendar2000

OBJECTS = c2000.o c2addevent.o c2grid.o c2misc.o c2showevent.o c2textwin.o
c2timer.o c2window.o c2event.o file.o

INCL=-I/usr/local/include -I/usr/local/lib/wx/include -I/usr/X11R6/include
-I/usr/local/lib/glib/include
DEFINES=-D__WXGTK__ -DLINUX -DGTK_NO_CHECK_CASTS
LIBS=-L/usr/lib -lwx_gtk

DEBUG=-g -pg -D__WXDEBUG__

# implementation

.SUFFIXES:      .o .cpp

.cpp.o :

all: $(PROGRAM)

$(PROGRAM):     $(OBJECTS)
        $(CC) -o $(PROGRAM) $(OBJECTS) $(LIBS) $(DEBUG)

clean: $(OBJECTS)
        rm -f $(OBJECTS) $(PROGRAM)

-----------------------

Make sure that the blanks in front of $(CC) is a single tab character and not
spaces! Otherwise make will complain.

PROGRAM is your outpout binary name.
OBJECTS are the objectfiles that have to be created. In your case it is main.o
INCL defines all the includepaths needed, just edit them
DEFINES can be cleared if you don't need it. DOn't delete the symbol but
remove all the defines instead. So you can use it later if you need it.
LIBS is where your libraries must be listed. The important rule here is -L
specifies the directory where the library is and -l specifies the library
itself without any extension or lib in front of the name. Also if you specify
more than one library that is in the same path then you must first set the -L
for the path and then all the libraries in that path. If you have to use
multiple paths for different libraries you always must specify the -L for a
given path first.

i.E.:

-L/mylibs -llib1 -llib2 -llib3 -L/syslibs -l syslib1 -l syslib2 -L/SDL -lsdl1
-lsdl2 ...

I guess you get the idea.
You can not specify all -L first and then all -l parameters because the path
is searched only in the order you specify it.

I think that should be enough to get you going. :)

--
Gerhard Gruber

Fr jedes menschliche Problem gibt es immer eine einfache L?sung:
Klar, einleuchtend und falsch. (Henry Louis Mencken)

 
 
 

GCC newbie

Post by Peter T. Breue » Mon, 24 Feb 2003 07:17:51



Quote:> I tried to compile my program source (1 file only: main.cpp) with this line
> gcc -B /root/SDL-1.2.5/include main.cpp

-B? What? And why are you using gcc, not g++? Gcc is a c compiler, not
a c++ compiler. Though you can force it to use the c++ forntend,
you haven't. And it won't link properly.

Quote:> ERROR! gcc cannot find SDL.h! (this file is in path:
> /root/SDL-1.2.5/include)

That is NOT in your include search path. Nothing is. You didn't give an
include search path, so it'll be looking in the usual places. Nowehre
else.

Quote:> WHY?  (Line in Source Code:    #include "SDL.h")

What ya mean "WHY"? Why would you ever expect it to? And what's your -B
nonsense for? That's for looking up components of gcc itself! Good job
it never got as far as needing any.

Quote:> I copied this file (and sub-includes) in main.cpp path.
> GOOD!
> BUT now gcc has found undefined symbols!
> Functions like: sin cos glVertex3i SDL_Init

So?

Quote:> This means that gcc is not linking Libraries with object from Source Code.

Correct.

Quote:> What I can do?

Stop messing up. You are making a bollocks of a perfectly simple
operation.

Quote:> Please give me a correct gcc syntax.

Where would you get a false one from? Surely there are no examples in
the known universe accessible to you of false syntax for gcc?

And anyway, you should be using g++, not gcc.

I would imagine you want

  g++ -I /root/SDL-1.2.5/include main.cpp

but I have no idea wha you want to really have as an include path, or
why.

Quote:> I use OpenGL, GLU, <math.h>, SDL.

Then you really want to link with the appropriate libraries
corresponding to them.

Peter

 
 
 

GCC newbie

Post by Tauno Voipi » Tue, 25 Feb 2003 00:07:52




Quote:> I tried to compile my program source (1 file only: main.cpp) with this
line

> gcc -B /root/SDL-1.2.5/include main.cpp

> ERROR! gcc cannot find SDL.h! (this file is in path:
> /root/SDL-1.2.5/include)
> WHY?  (Line in Source Code:    #include "SDL.h")

> I copied this file (and sub-includes) in main.cpp path.
> GOOD!
> BUT now gcc has found undefined symbols!
> Functions like: sin cos glVertex3i SDL_Init
> This means that gcc is not linking Libraries with object from Source Code.
> What I can do?

> Please give me a correct gcc syntax.
> I use OpenGL, GLU, <math.h>, SDL.

> I cannot create Makefile! I am too newbie!

In addition to the comments by others, please

use g++ to compile C++ code, use gcc for C.

Tauno Voipio

 
 
 

1. GCC newbie needs help, please!


Try the following:

#include <stdio.h>
#include <ncurses/ncurses.h>

main()
{
 initscr();  /*Otherwise you may get a segmentation fault*/
 clear();
 addstr("Hello World!\n");
 refresh();
 getch();
 clear();
 refresh();  /*if you really want the screen cleared.*/
 endwin();

gcc helloworld.c -o hello -lncurses -I/usr/include/ncurses

The -I/usr/include/ncurses tells gcc to look in the /usr/include/ncurses
directory for any include files (such as unctrl.h), that are not found
in the normal location for include files (/usr/include).  You can have
several -I parms passed to gcc, just like you can link several libraries
with -l.

If you don't want to use ncurses, try the following:

#include <stdio.h>

main()
{
 printf("Hello World!\n");

gcc helloworld.c -o hello

I hope you find that helpful.

Andrew L. Sandoval

/*My comments are my own, and not Novell's.  Use at your own risk...(like there is any!)*/

2. Xforms libraries

3. Linux/GCC Newbie question

4. Old-timers: question on NCD terminal

5. Linux/gcc Newbie Question:

6. Blind Linux Boxes

7. Using inw() with GCC - newbie

8. Coding in Unix

9. gcc-newbie question...

10. using gcc (newbie)

11. newbie GCC problem

12. gcc Installation (NewBie)

13. Newbie Q re gcc & undefined symb err msg