LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-23-2004, 05:42 AM   #1
Hairein
LQ Newbie
 
Registered: Sep 2004
Posts: 6

Rep: Reputation: 0
Unhappy GCC + STL Compile Problem


Hi all,
I can't get the following code to compile under SUSE Linux 9.1, using GCC 3.3.3 or GCC 3.3.1, what am I missing here?

This is my example CPP:

#include <iostream>
#include <string>

int main(int argc,char **argv)
{
std::string testString= "hello world";
std::cout << testString<< std::endl;

return 0;
}

and the Makefile is:

TestSTL: TestSTL.o
ld -L"/usr/lib" -L"/usr/local/lib"\
TestSTL.o\
-o TestSTL

TestSTL.o: TestSTL.cpp
gcc -g -O0 -I"." -I"/usr/include/g++" -I"/usr/include/g++/bits"\
-I"/usr/include/g++/ext"\
-c TestSTL.cpp -o TestSTL.o

clean:
rm -f *.o
rm TestSTL

If anyone would be so nice as to correct the code or makefile, I would be grateful! It looks so simple, and it works under Windows using VC 2003
and VS 2005 Beta. However GCC 3.3.1 Cygwin on Windows fails too.

Greetings.
 
Old 09-23-2004, 08:03 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
There's nothing wrong with the code itself. The problem is that you're using gcc to compile a c++ program. Use g++ instead, that's the c++ compiler in gcc (gnu compiler collection). GCC contains a number of compilers and on some systems, the compilers for each language must be installed separately (I know this is the case in Cygwin). And btw, that's a very complicated Makefile for that program and you shouldn't have to set paths like that if your system is setup correctly.

if your program source file name test.cpp, you could simply do:
g++ -o test test.cpp
(run with $ ./test)
or just make test (no Makefile required)
But I like to compile mine with -W and -Wall to get more warnings about potential problems.
A Makefile could look like (for a c++ program):
CXX = g++
LD = g++
CXXFLAGS = -Wall -W -ansi -pedantic -g -ggdb -c -o
LDFLAGS = -o $(EXEC)
EXEC = test
OBJECTS = test.o

all: $(OBJECTS)
<tab>$(LD) $(OBJECTS) $(LDFLAGS)

%.o: %.cpp
<tab>$(CXX) $(CXXFLAGS) $@ $<

clean:
<tab>rm -f $(OBJECTS) $(EXEC)

This is the makefile template I use for most of my c++ programs (when making debug versions). It's not necessary for a program with only one source file. If you need to link with any libraries not in your path, add those to LDFLAGS.
 
Old 09-23-2004, 08:18 AM   #3
Hairein
LQ Newbie
 
Registered: Sep 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Not quite there yet...

Hi again,
thanks for the advice regarding the compiler and the makefile. However, after replacing the gcc call with g++
I get the following output from the compile, which is the same as before:

> make TestSTL.o
g++ -g -O0 -I"." -I"/usr/include/g++" -I"/usr/include/g++/bits"\
-I"/usr/include/g++/ext"\
-c TestSTL.cpp -o TestSTL.o
In file included from /usr/include/g++/ext/memory:67,
from /usr/include/g++/string:48,
from /usr/include/g++/bits/locale_classes.h:47,
from /usr/include/g++/bits/ios_base.h:47,
from /usr/include/g++/ios:49,
from /usr/include/g++/ostream:45,
from /usr/include/g++/iostream:45,
from TestSTL.cpp:1:
/usr/include/g++/bits/stl_tempbuf.h:78: error: type specifier omitted for
parameter `_ForwardIteratorConcept'
/usr/include/g++/bits/stl_tempbuf.h:80: error: parse error in method
specification before `_M_original_len'
/usr/include/g++/bits/stl_tempbuf.h:144: error: syntax error before `}' token
In file included from /usr/include/g++/string:48,
from /usr/include/g++/bits/locale_classes.h:47,
from /usr/include/g++/bits/ios_base.h:47,
from /usr/include/g++/ios:49,
from /usr/include/g++/ostream:45,
from /usr/include/g++/iostream:45,
from TestSTL.cpp:1:
/usr/include/g++/ext/memory:69: error: semicolon missing after declaration of `
std::_Temporary_buffer<_ForwardIterator, _Tp>'
/usr/include/g++/bits/stl_tempbuf.h:80: error: parse error at end of saved
function text
/usr/include/g++/ext/memory:70: error: ISO C++ forbids defining types within
return type
/usr/include/g++/ext/memory:70: error: syntax error before `{' token
/usr/include/g++/ext/memory:70: confused by earlier errors, bailing out
make: *** [TestSTL.o] Fehler 1

I got this example to compile, but not link, using the new Intel C++ Compiler v8.1 (icgc). Did anyone manage to compile the code given above successfully? I would really need to get the changed CPP or Makefile, so that I can try it out on my box here.

Thanks in advance.
 
Old 09-23-2004, 08:20 AM   #4
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Can you compile your program with just
g++ -o foo <nameofyoursourcefileincludingextension>
?
 
Old 09-23-2004, 08:24 AM   #5
Hairein
LQ Newbie
 
Registered: Sep 2004
Posts: 6

Original Poster
Rep: Reputation: 0
That works fine!

Hello,
using g++ -o foo TestSTL.cpp
works fine for me. It compiles, links & runs without problems.

Needless to say, i'm a bit gobsmacked at this point!

Do you know why this works and the other method using the makefile doesn't?

Yours questioningly.
 
Old 09-23-2004, 08:35 AM   #6
Hairein
LQ Newbie
 
Registered: Sep 2004
Posts: 6

Original Poster
Rep: Reputation: 0
Solved!

As far as I can tell, the problems were:
1) Using gcc instead of g++
2) Including the following into the Makefile:
-I"/usr/include/g++" -I"/usr/include/g++/bits"\
-I"/usr/include/g++/ext"\
3) Using ld instead of calling g++ again with just the *.o files as such:
# makefile for TestSTL
TestSTL: TestSTL.o
g++ -L"/usr/lib" -L"/usr/local/lib" TestSTL.o -o TestSTL

TestSTL.o: TestSTL.cpp
g++ -g -O0 -I"." -c TestSTL.cpp -o TestSTL.o

clean:
rm -f *.o
rm TestSTL

Although I don't understand the cause & effect here, I can continue development on
my main issue, so thankyou very much for your help!

Regards.
 
Old 09-23-2004, 08:43 AM   #7
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Yeah, that's another problem when one tries to use ld directly to perform the link, never do that. If you're writing C code, always set gcc as LD in your makefile. If you're writing c++ code, always set g++ as LD in your makefile (they will invoke ld itself when performing the actual link and they now how to invoke it properly). And don't try to include paths to libraries or headers and whatnot that are visible by default to your compiler.
Glad things are working for you now.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Qmail Compile Problem with GCC-3.3 Richard7890 Linux - Software 3 06-22-2005 07:17 AM
compile error while returning STL iterator as object * rameshmiraje Programming 2 06-20-2004 01:50 PM
Problem due to STL Library vipinsharma Programming 9 06-01-2004 03:46 AM
gcc STL include problem finidi Programming 3 01-14-2003 06:55 AM
C++ STL problem jaycee999 Programming 5 05-31-2002 03:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration