ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.