How to install gfortran under macports and use it with cmake? - 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.

Related

CMake -- CUDA compiler is not able to compile a simple test program

I am working on a small library that makes use of CUDA, however I cannot seem to make CMake work when I run it from the command line, even though my IDE (CLion) uses CMake to comile the program succesfully.
I have scoured the internet for ages trying to solve this, though with nothing making much of a difference. I have got CUDA 11.1 and CMake 3.17.3, and both are accessable from the command line.
CLion is able to compile the code perfectly, and everything works as expected, however when I run CMake from the command line, it gives the following error:
PS C:\Users\penci\OneDrive\Desktop\Rapid\Temporary\Rapid\build> cmake ..
-- The CUDA compiler identification is NVIDIA 11.1.105
-- Check for working CUDA compiler: /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe
-- Check for working CUDA compiler: /cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe - broken
CMake Error at /usr/share/cmake-3.17.3/Modules/CMakeTestCUDACompiler.cmake:46 (message):
The CUDA compiler
"/cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe"
is not able to compile a simple test program.
It fails with the following output:
Change Dir: /cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make.exe cmTC_2c864/fast && /usr/bin/make -f CMakeFiles/cmTC_2c864.dir/build.make CMakeFiles/cmTC_2c864.dir/build
make[1]: Entering directory '/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp'
Building CUDA object CMakeFiles/cmTC_2c864.dir/main.cu.o
"/cygdrive/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v11.1/bin/nvcc.exe" -forward-unknown-to-host-compiler -x cu -c /cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp/main.cu -o CMakeFiles/cmTC_2c864.dir/main.cu.o
c1xx: fatal error C1083: Cannot open source file: 'C:/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp/main.cu': No such file or directory
main.cu
make[1]: *** [CMakeFiles/cmTC_2c864.dir/build.make:86: CMakeFiles/cmTC_2c864.dir/main.cu.o] Error 2
make[1]: Leaving directory '/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:141: cmTC_2c864/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeOutput.log".
See also "/cygdrive/c/Users/penci/OneDrive/Desktop/Rapid/Temporary/Rapid/build/CMakeFiles/CMakeError.log".
Sorry for the wall of text, but I'm not sure why this doesn't work in the command line, when it does in CLion.
This is my CMakeLists.txt file:
cmake_minimum_required(VERSION 3.17)
project(Rapid LANGUAGES CUDA)
set(CMAKE_CUDA_STANDARD 14)
include_directories(${PROJECT_SOURCE_DIR}/include/rapid/graphics/GLFW)
link_directories(${PROJECT_SOURCE_DIR}/include/rapid/graphics/GLFW/lib64)
set(CMAKE_CUDA_FLAGS "${CMAKE_CUDA_FLAGS} -Xcompiler /openmp")
SET (CMAKE_C_COMPILER_WORKS 1)
SET (CMAKE_CXX_COMPILER_WORKS 1)
add_executable(Rapid main.cu)
set_target_properties(
Rapid
PROPERTIES
CUDA_SEPARABLE_COMPILATION ON)
target_link_libraries(Rapid cublas glfw3 gdi32 opengl32)
Any ideas as to how I can fix this issue would be greatly appreciated, as I have spent days trying to fix this, but nothing online seems to work.
After Googling some things that seemed completely unrelated to the issue, I discovered that my Cygwin64 install of CMake was not setup to use Visual Studio generators.
To fix the issue, I needed to put the Visual Studio install of CMake into the PATH environment variable, but it needed to be above the Cygwin64 bin directory. This meant that running the cmake command from the command line would refer to the Visual Studio install, and not the Cygwin64 install, allowing it to work correctly.
Hopefully this helps someone else in the future too

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

Trying to use JetBrains CLion, but Cmake - c++ compiler broken after cygwin install

I have been trying to get JetBrains CLion IDE working, but I am running into a problem with Cmake and none of my research online has been helpful. I installed cygwin with the necessary packages (as stated here - gcc/g++, cmake, make, gdb). When I open up CLion and go into Settings (I'm running Windows 10) under Build>Toolchains>Debugger everything has a check next to it, so it seems like CLion has found everything alright, but when I look in the Debugger window I get the following error:
"C:\Users\Lucas Lofaro\.CLion2016.3\system\cygwin_cmake\bin\cmake.exe" -DCMAKE_BUILD_TYPE=Debug -G "CodeBlocks - Unix Makefiles" "/cygdrive/c/Users/Lucas Lofaro/ClionProjects/cracking_the_coding_interview"
-- The CXX compiler identification is GNU 5.3.0
-- Check for working CXX compiler: /usr/bin/c++.exe
-- Check for working CXX compiler: /usr/bin/c++.exe -- broken
CMake Error at /cygdrive/c/Users/Lucas Lofaro/.CLion2016.3/system/cygwin_cmake/share/cmake-3.6.2/Modules/CMakeTestCXXCompiler.cmake:54 (message):
The C++ compiler "/usr/bin/c++.exe" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: /cygdrive/c/Users/Lucas Lofaro/ClionProjects/cracking_the_coding_interview/cmake-build-debug/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make.exe" "cmTC_96921/fast"
/usr/bin/make -f CMakeFiles/cmTC_96921.dir/build.make
CMakeFiles/cmTC_96921.dir/build
make[1]: Entering directory '/cygdrive/c/Users/Lucas
Lofaro/ClionProjects/cracking_the_coding_interview/cmake-build-debug/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_96921.dir/testCXXCompiler.cxx.o
/usr/bin/c++.exe -o CMakeFiles/cmTC_96921.dir/testCXXCompiler.cxx.o -c
"/cygdrive/c/Users/Lucas
Lofaro/ClionProjects/cracking_the_coding_interview/cmake-build-debug/CMakeFiles/CMakeTmp/testCXXCompiler.cxx"
Linking CXX executable cmTC_96921.exe
"/cygdrive/c/Users/Lucas
Lofaro/.CLion2016.3/system/cygwin_cmake/bin/cmake.exe" -E cmake_link_script
CMakeFiles/cmTC_96921.dir/link.txt --verbose=1
/usr/bin/c++.exe -Wl,--enable-auto-import
CMakeFiles/cmTC_96921.dir/testCXXCompiler.cxx.o -o cmTC_96921.exe
-Wl,--out-implib,libcmTC_96921.dll.a
-Wl,--major-image-version,0,--minor-image-version,0
c++: fatal error: -fuse-linker-plugin, but cyglto_plugin.dll not found
compilation terminated.
CMakeFiles/cmTC_96921.dir/build.make:97: recipe for target 'cmTC_96921.exe'
failed
make[1]: *** [cmTC_96921.exe] Error 1
make[1]: Leaving directory '/cygdrive/c/Users/Lucas
Lofaro/ClionProjects/cracking_the_coding_interview/cmake-build-debug/CMakeFiles/CMakeTmp'
Makefile:126: recipe for target 'cmTC_96921/fast' failed
make: *** [cmTC_96921/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:2 (project)
-- Configuring incomplete, errors occurred!
See also "/cygdrive/c/Users/Lucas Lofaro/ClionProjects/cracking_the_coding_interview/cmake-build-debug/CMakeFiles/CMakeOutput.log".
See also "/cygdrive/c/Users/Lucas Lofaro/ClionProjects/cracking_the_coding_interview/cmake-build-debug/CMakeFiles/CMakeError.log".
I've also added the cygwin bin directory to my Path environment variable, but I am really at a loss as to what to do next. Something isn't linking up and I don't want to start digging around in Cmake files without a better understanding of what's going on. Any guidance would be much appreciated. Please let me know if I can provide any additional information.
It turns out that the cmake, make, and g++ packages were installed but not updated to their most recent versions. Rerunning cygwin installer solved my problem and everything now compiles fine.

CMake: satisfying dependencies when building shared object file (.so) from object file (.o)

I am able to do this without CMake using a handwritten Makefile, like so:
g++ $(CXSCINC) -c -fPIC cellComplex_extern.cpp -o cellComplex_extern.o
g++ $(CXSCINC) -shared -Wl -o cellComplex_lib.so cellComplex_extern.o $(CXSCLIB) -lcxsc
This gets me shared library cellComplex_lib.so, which then gets picked up by ctypes as a dynamically linked library (lib = ctypes.cdll.LoadLibrary('./cellComplex_lib.so') for later use.
My project has moved to CMake as a build system and I am looking to emulate the functionality of my Makefile above.
So far I have discovered the add_library() command for CMakeLists.txt, but the link to the CXSC library is never made (as evidenced by horrible complaining when I run make after cmake.
How can I tell CMake to build cellComplex_lib with the third-party library CXSC?
-- non-working CMakeLists.txt --
add_library(include/python/cellComplex_extern OBJECT
include/python/cellComplex_extern.cpp ${all_headers})
add_library(include/python/cellComplex_lib SHARED
include/python/cellComplex_extern)
target_link_libraries(include/python/cellComplex_lib ${CXSC_LIB_DIR}/libcxsc.a)
Result of running cmake followed by make:
.
.
.
[ 75%] Built target include/python/cellComplex_extern
Linking CXX shared library libinclude/python/cellComplex_lib.dylib
ld: can't open output file for writing: libinclude/python/cellComplex_lib.dylib, errno=2 for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [libinclude/python/cellComplex_lib.dylib] Error 1
make[1]: *** [CMakeFiles/include/python/cellComplex_lib.dir/all] Error 2
make: *** [all] Error 2
I think you need to use target_link_libraries
target_link_libraries(include/python/cellComplex_lib ${CXSLIB})
This is what I use during Win32 development:
link_directories(${LIB_ROOT_DIR}/lib ${LIB_ROOT_DIR2}/lib/morelibs)
add_library(MyDll1 SHARED File1.cpp File2.cpp)
add_library(MyDll2 SHARED File3.cpp File4.cpp)
add_dependencies(MyDll2 Dll1)
target_link_libraries(MyDll2 Dll1 some.lib another.lib)
Here you specify that Dll2 requires Dll1 and two other external lib's.

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?