Linking error with g++ 3.4.4 and g++ 3.4.5 - g++

Using: windows xp, g++ 3.4.4 with cygwin and g++ 3.4.5 with mingw.
I'm compiling a simple unit test class with cppunit.
When I link using g++ 3.4.5 I get a lot of linking errors. When I link with g++ 3.4.4 I don't get any errors and the exe links fine and runs.
I can't seem to trace down the errors, so any thoughts?
Thanks.
EDIT: linking errors: Unreferenced function errors. Like:
SimpleTest.cpp:(.text+0x313): undefined reference to `CppUnit::Message::Message(std::string const&, std::string const&)'
EDIT: cmd line:
g++ -I g:\projects\thirdparty\cppunit-1.12.1\include -L g:\projects\thirdparty\cppunit-1.12.1\lib -l cppunitd -o main.exe main.cpp SimpleTest.cpp
Update: Same code in Visual Studio: No error, unit test runs as expected.

Your problem is likely incorrect link line. The order of sources/object files and libraries on the link line matters. Correct link line:
g++ -I g:\projects\thirdparty\cppunit-1.12.1\include \
-L g:\projects\thirdparty\cppunit-1.12.1\lib \
-o main.exe main.cpp SimpleTest.cpp -lcppunitd

As g++ matures, I'm on 4.2.3, it's type checking has gotten more pedantic and for that matter better. With the little information I'd say it's likely that you should look closely at your calls to these methods. I suspect that the types are not quite right. 3.4.4 doesn't catch it, 3.4.5 does.
....JW

One thing you could perhaps try is to compile with g++ 3.4.5 and/or 3.4.4 on Linux. If the result then is the same, then it is clearly a property of gcc. Otherwise it more sounds like a mingw issue.

Related

Lapack undefined reference

I am new to g++ and lapack, and attempting to use them. I encountered a problem when I tried to compile the following naive code
#include <lapackpp.h>
int main()
{
LaGenMatDouble A;
return 0;
}
If I run the command
$g++ -L/usr/local/lib -llapackpp test2.cpp
where test2.cpp is the name of the cpp file, the terminal would give an error:
test2.cpp:1:22: fatal error: lapackpp.h: No such file or directory
But if I run the command:
$g++ -I/usr/local/include/lapackpp -L/usr/local/lib -llapackpp test2.cpp
the terminal would give an error:
/tmp/ccUi11DG.o: In function `main':
test2.cpp:(.text+0x12): undefined reference to `LaGenMatDouble::LaGenMatDouble()'
test2.cpp:(.text+0x23): undefined reference to `LaGenMatDouble::~LaGenMatDouble()'
collect2: ld returned 1 exit status
BTW, if I run the command
$pkg-config lapackpp --libs
the result is
-L/usr/local/lib -llapackpp
Could you please help me solve this? Thanks in advance!
Lapack requires fortran libraries, so that's where the -lgfortran comes from. Moreover, it appears the exact way to provide that library for the compiler depends on the Linux distriburion. From the documentation:
Requirements
This package requires the packages "blas", "lapack" (without the "++"), and a Fortran compiler. On most Linuxes these are available as pre-compiled binaries under the name "blas" and "lapack". For SuSE 10.x, the Fortran compiler is available as package "gfortran". For SuSE 9.x, the Fortran compiler is available as package "gcc-g77".
Not sure why pkg-config lapackpp --libs does not list -lgfortran
The -I/usr/local/include/lapackpp specifes the lapackpp-related header files. Without it the compiler cannot find lapackpp.h when you try to include it (#include <lapackpp.h>) -- see the compiler error in your question
I finally solved the problem but would still wonder why it has to be so.
The only command that can link cpp file to lapackpp library is:
g++ foo.cpp -o foo -lgfortran -llapackpp -I/usr/local/include/lapackpp
It would not work without -lgfortran, or with -I/usr/local/include/lapackpp replaced by -L/usr/local/lib.
Does anyone have an answer?

Problems when compiling Objective C with Clang (Ubuntu)

I'm learning Objective-C language. Since I don't have a Mac, I'm compiling and running my code within Ubuntu 11.04 platform.
Until now, I was using gcc to compile. I've installed GNUStep and all was working. But then I started to try some Objective-C 2.0 features, like #property and #synthesize, that gcc does not allow.
So I tried to compile the code with Clang, but it seems that it is not correctly linking my code with the GNUStep libraries, not even with a simple Hello world program.
For example, if I compile the following code:
#import <Foundation/Foundation.h>
int main(void) {
NSLog(#"Hello world!");
return 0;
}
The output of the compiler is:
/tmp/cc-dHZIp1.o: In function `main':
test.m:(.text+0x1f): undefined reference to `NSLog'
/tmp/cc-dHZIp1.o: In function `.objc_load_function':
test.m:(.text+0x3c): undefined reference to `__objc_exec_class'
collect2: ld returned 1 exit status
clang: error: linker (via gcc) command failed with exit code 1 (use -v to see invocation)
The command I'm using to compile is
clang -I /usr/include/GNUstep/ test.m -o test
with the -I directive to include the GNUStep libraries (otherwise, Clang is not able to find Foundation.h).
I've googled my problem, and visited both GNUStep and Clang web pages, but I haven't found a solution to it. So any help will be appreciated.
Thanks!
The problem was that the library gnustep-base was not being used by the linker. So the solution to this was using the option -Xlinker, that sends arguments to the linker used by clang:
clang -I /usr/include/GNUstep/ -Xlinker -lgnustep-base test.m -o test
The statement "-X linker -lgnustep-base" made the magic. However, I had problems with this command related to the class that represents a string in Objective-C:
./test: Uncaught exception NSInvalidArgumentException, reason: GSFFIInvocation:
Class 'NXConstantString'(instance) does not respond to forwardInvocation: for
'hasSuffix:'
I could solve it adding the argument "-fconstant-string-class=NSConstantString":
clang -I /usr/include/GNUstep/ -fconstant-string-class=NSConstantString \
-Xlinker -lgnustep-base test.m -o test
In addition, I've tried with some Objective-C 2.0 pieces of code and it seems to work.
Thank you for the help!
You can try gcc compiler:
First of all install GNU Objective-C Runtime: sudo apt-get install gobjc
then compile: gcc -o hello hello.m -Wall -lobjc
You are not able to use ObjC 2.0 features because you're missing a ObjC-runtime supporting those. GCC's runtime is old and outdated, it doesn't support ObjC 2.0. Clang/LLVM doesn't have a acompanied runtime, you need to install the ObjC2-runtime from GNUstep (which can be found here: https://github.com/gnustep/libobjc2 ) and reinstall GNUstep using this runtime.
Here are some bash scripts for different Ubuntu versions, that do everything for you:
http://wiki.gnustep.org/index.php/GNUstep_under_Ubuntu_Linux
And please don't try to reinvent GNUstep make, instead, use it:
http://www.gnustep.org/resources/documentation/Developer/Make/Manual/gnustep-make_1.html
If you really don't think so, here is some excerpt from there:
1.2 Structure of a Makefile
Here is an example makefile (named GNUmakefile to emphasis the fact that it relies on special features of the GNU make program).
#
# An example GNUmakefile
#
# Include the common variables defined by the Makefile Package
include $(GNUSTEP_MAKEFILES)/common.make
# Build a simple Objective-C program
TOOL_NAME = simple
# The Objective-C files to compile
simple_OBJC_FILES = simple.m
-include GNUmakefile.preamble
# Include in the rules for making GNUstep command-line programs
include $(GNUSTEP_MAKEFILES)/tool.make
-include GNUmakefile.postamble
This is all that is necessary to define the project.
In your case replace all occurrences of simple with test and you're done
1.3 Running Make
Normally to compile a package which uses the Makefile Package it is purely a matter of typing make from the top-level directory of the package, and the package is compiled without any additional interaction.

undefined reference to `SetTextColor#8'

I got the "undefined reference to `SetTextColor#8' " error while compiling a c++ program using g++ under windows. The complete error log is here http://pastebin.com/DCwHKkeE
I guess i am missing some library file but don't know which ones. Please help
The SetTextColor func is defined in gdi32.lib according to the MSDN docs, so you'll need to link libgdi32.a in mingw.
Just link the libgdi32 and it should be fixed.
g++ -g main.cpp -o main.exe -lgdi32 -lmingw32

Foundation/NSObject.h: No such file or directory what the reason on windows

When I compiled an Objective-C code on Windows, I got this error.
$ gcc -o abc main.m
In file included from MyClass.m:2,
from main.m:2:
myclass.h:1:32: Foundation/NSObject.h: No such file or directory
In file included from MyClass.m:2,
from main.m:2:
myclass.h:4: error: syntax error before '(' token
myclass.h:6: error: cannot find interface declaration for `NSObject', superclass of `myclass'
Why is that? How can I fix this?
Thanks to initiate my help dear frnds
i hv just got the answer after posting this question. so the command to compile in window environment is this..
gcc `gnustep-config --objc-flags` -o myf main.m -L /GNUstep/System/Library/Libraries -lobjc -lgnustep-base
thanks for all.
You say you're on Windows. Do you in fact have an environment (such as GNUstep or Cocotron) that would give you the Foundation framework installed and set up correctly? Because I would guess not, and not having Foundation would indeed lead to errors about not having Foundation.

g++ issue with Magick++ and cygwin

When I try to compile a simple c++ file using Magick++ and cygwin, I keep getting this result:
$ g++ -o imageTest imageTest.cpp `GraphicsMagick++-config --cppflags --cxxflags --ldflags --libs`
g++: unrecognized option `-no-undefined'
/usr/lib/gcc/i686-pc-cygwin/3.4.4/../../../../i686-pc-cygwin/bin/ld: cannot find -ldpstk
collect2: ld returned 1 exit status
I installed ImageMagick through the cygwin gui setup.
GraphicsMagick and ImageMagick are two different libraries. If you want to build your program using ImageMagick, as you state, it's just a matter of changing
GraphicsMagick++-config
into
Magick++-config
This should work. As for GraphicsMagick, it looks like the current -devel library in Cygwin is broken, as it requires a library (libdpstk) which is no longer available. (Have a look here if you are interested.)