Lapack undefined reference - g++

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?

Related

g++ doesn't recognize the file format of a dll

I am on Windows, and I am using the version of g++ that comes with mingw-64. I have a file on my computer called lua51.dll. When I try to run the following command :
g++ -shared -fPIC -o stuff.dll -llua51 stuff.cpp
I get the following error:
C:/Program Files/LOVE/lua51.dll: file not recognized: File format not recognized
collect2.exe: error: ld returned 1 exit status
make: *** [main] Error 1
How can I fix this? Please let me know if more information is needed; I am a complete beginner to compilation.
This is an issue you get when you try to include a 64-bit library when running a 32-bit version of g++ or even gcc.
I thought that that the version of g++.exe that comes with mingw-64 would produce 64-bit code, but it turns out this isn't true; it still only produces 32-bit code. In my case, lua51.dll is 64-bit, which is an issue since I was using the version of g++ that produces 32-bit code.
Instead, you need to use x86_64-w64-mingw32-g++.exe, which can be found in the same folder as g++.exe. This is the version of g++ that will produce 64-bit code.

msys2 (msys64) cannot find -lcrypt

I have msys2 (msys64) https://www.msys2.org/ and am compiling some older code in gcc, but get "cannot find -lcrypt", -lcrypt is declared in the makefile, as crypt is cited in the code.
The only version of crypt library I can get to get to run is -lcrypt32 which is a library in msys2, but this doesn't seem to offer the support for crypt so I then get crypt errors. Does anyone know how to get the normal crypt recognized in msys2? I assume it's relying on mingw, I also recall from a long while back that mingw doesn't support lcrypt, but I could be wrong.
/usr/lib/gcc/x86_64-pc-msys/6.4.0/../../../../x86_64-pc-msys/bin/ld: cannot find -lcrypt
collect2: error: ld returned 1 exit status
make: *** [Makefile:260: arch] Error 1
MSYS2 does not appear to ship a MinGW version of libcrypt. But as long as you just want to run this locally and do not need to redistribute it, you could compile it with the MSYS2 GCC. There is libcrypt for it. Just run...
pacman -S msys/libcrypt-devel
And then that should work:
gcc test.c -lcrypt

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.

How to install gfortran under macports and use it with cmake?

hope someone can me help. I need to compile some code. I installed everything I needed with macports, in /opt/local/. And it's working how it should, except gFortran. I get this error:
ld: library not found for -lgfortran
collect2: ld returned 1 exit status
make[2]: *** [vigranumpy/private/graph/tws/svs.dylib] Error 1
make[1]: *** [vigranumpy/private/graph/tws/CMakeFiles/svs.dir/all] Error 2
make: *** [all] Error 2
I want everything to be installed in /opt/local/, because I don't want to touch the system (/usr/). gFortran isn't available for macports. You can install it with gcc46 as a variant. But if I use the gcc46 instead the default compilers, then the code before want compile.
How can I fix that?
Kind regards
See the CMake FAQ on how to use a different compiler:
http://www.cmake.org/Wiki/CMake_FAQ#How_do_I_use_a_different_compiler.3F
To use gfortran from macports as the fortran compiler, you should:
export FC=/opt/local/bin/gfortran
export CC=/opt/local/bin/gcc
export CXX=/opt/local/bin/g++
...prior to calling CMake. Then, after calling CMake with such environment variables set, it will cache the compiler paths in the CMakeCache.txt file, so for subsequent runs, you do not need the environment variables set anymore.
For mixed language (C, C++, Fortran) projects, it's important that the compilers all play nicely with each other.
This advice only works with the "Unix Makefiles" generator. I do not know of anybody who is using fortran via Xcode in conjunction with CMake.

LD: linking with STL libraries

I was trying to compile a OpenCV's VideoCapture example. When I compile it, I get the following output:
gpp test.c
Info: resolving vtable for cv::VideoCapture by linking to __imp___ZTVN2cv12VideoCaptureE (auto-import)
c:/programs/mingw/bin/../lib/gcc/mingw32/4.5.0/../../../../mingw32/bin/ld.exe: warning: auto-importing has
enable-auto-import specified on the command line.
This should work unless it involves constant data structures referencing symbols from auto-imported DLLs.
(Btw, gpp is an alias to g++ -lhighgui -lcv -lcxcore)
So, I tried to compile with "gpp --enable-auto-import", but g++ didn't recognize this option. So, I tried to compile like this:
gpp -c test.c
ld test.o
And I've got the same error AND many other errors about STL functions, indicating it didn't link with STL:
undefined reference to std::cout
...
And, finally, when I compiled like this:
gpp -c test.c
ld --enable-auto-import test.o
This time, I've only got the STL errors. The VideoCapture error is gone! So I guess I solved this problem. The only thing is: how do I make ld link my program with STL libraries??????
Thanks in advance
The correct solution is build with
g++ test.c -lhighgui -lcv -lcxcore -Wl,--enable-auto-import
Unlike your 'gpp' alias, this puts libraries after objects which reference them (important when linking with archive libraries), and also properly passes --enable-auto-import flag to the linker.
Your current "fix" only works "by accident".