CMake 3.0+Fortran+CUDA requires -fPIC even when building executables - cmake

This is a CMake question. I cannot compile Fortran executables with CUDA support when using the Intel Fortran compiler, unless I include the -fPIC flag. The problem is that -fPIC shouldn't be necessary unless I'm building a library.
The following is minimal example:
# CMakeLists.txt
cmake_minimum_required(VERSION 3.0)
project(cuda LANGUAGES Fortran CXX)
find_package(CUDA)
cuda_add_executable(main main.f90)
and
# main.f90
end
When I try to build and run,
cmake -D CMAKE_Fortran_COMPILER=ifort .. && make VERBOSE=1
I get the following:
-- The Fortran compiler identification is Intel 18.0.0.20170811
-- The CXX compiler identification is GNU 7.3.0
-- Check for working Fortran compiler: /opt/intel/compilers_and_libraries_2018.0.128/linux/bin/intel64/ifort
-- Check for working Fortran compiler: /opt/intel/compilers_and_libraries_2018.0.128/linux/bin/intel64/ifort -- works
-- Detecting Fortran compiler ABI info
-- Detecting Fortran compiler ABI info - done
-- Checking whether /opt/intel/compilers_and_libraries_2018.0.128/linux/bin/intel64/ifort supports Fortran 90
-- Checking whether /opt/intel/compilers_and_libraries_2018.0.128/linux/bin/intel64/ifort supports Fortran 90 -- yes
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for C++ include pthread.h
-- Looking for C++ include pthread.h - found
-- Looking for pthread_create
-- Looking for pthread_create - not found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found CUDA: /opt/cuda (found version "9.1")
-- Configuring done
-- Generating done
-- Build files have been written to: /home/raul/tmp/cuda/build
/usr/bin/cmake -H/home/raul/tmp/cuda -B/home/raul/tmp/cuda/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/raul/tmp/cuda/build/CMakeFiles /home/raul/tmp/cuda/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/raul/tmp/cuda/build'
make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/depend
make[2]: Entering directory '/home/raul/tmp/cuda/build'
cd /home/raul/tmp/cuda/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/raul/tmp/cuda /home/raul/tmp/cuda /home/raul/tmp/cuda/build /home/raul/tmp/cuda/build /home/raul/tmp/cuda/build/CMakeFiles/main.dir/DependInfo.cmake --color=
Dependee "/home/raul/tmp/cuda/build/CMakeFiles/main.dir/DependInfo.cmake" is newer than depender "/home/raul/tmp/cuda/build/CMakeFiles/main.dir/depend.internal".
Dependee "/home/raul/tmp/cuda/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/raul/tmp/cuda/build/CMakeFiles/main.dir/depend.internal".
Scanning dependencies of target main
make[2]: Leaving directory '/home/raul/tmp/cuda/build'
make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/requires
make[2]: Entering directory '/home/raul/tmp/cuda/build'
make[2]: Nothing to be done for 'CMakeFiles/main.dir/requires'.
make[2]: Leaving directory '/home/raul/tmp/cuda/build'
make -f CMakeFiles/main.dir/build.make CMakeFiles/main.dir/build
make[2]: Entering directory '/home/raul/tmp/cuda/build'
[ 50%] Building Fortran object CMakeFiles/main.dir/main.f90.o
/opt/intel/compilers_and_libraries_2018.0.128/linux/bin/intel64/ifort -I/opt/cuda/include -c /home/raul/tmp/cuda/main.f90 -o CMakeFiles/main.dir/main.f90.o
[100%] Linking CXX executable main
/usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
/usr/bin/c++ -rdynamic CMakeFiles/main.dir/main.f90.o -o main /opt/cuda/lib64/libcudart_static.a -lpthread -ldl -lrt -lifport -lifcoremt -limf -lsvml -lipgo -lirc -lpthread -lsvml -lirc_s -ldl
/usr/bin/ld: CMakeFiles/main.dir/main.f90.o: relocation R_X86_64_32 against `.rodata' can not be used when making a shared object; recompile with -fPIC
/usr/lib/gcc/x86_64-pc-linux-gnu/7.3.0/../../../../lib/Scrt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/usr/bin/ld: final link failed: Invalid operation
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:97: main] Error 1
make[2]: Leaving directory '/home/raul/tmp/cuda/build'
make[1]: *** [CMakeFiles/Makefile2:69: CMakeFiles/main.dir/all] Error 2
make[1]: Leaving directory '/home/raul/tmp/cuda/build'
make: *** [Makefile:84: all] Error 2
I do not get the build error if I use the newer CUDA features of CMake, e.g.
# CMakeLists.txt
cmake_minimum_required(VERSION 3.10)
project(cuda LANGUAGES Fortran CUDA)
add_executable(main main.f90)
However, our current policy is to support CMake 3.0 and thus I cannot the simpler version. Also, I do not get any error if instead of ifort I use gfortran. I'm currently looking into FindCUDA.cmake that ships with CMake, but I haven't found a solution yet. Any thoughts?

I found a solution that works for now, but it's not trivial at all. First, one should use icpc with ifort (not a requirement for CMake >=3.8). It turns out that out of the many libraries that icpc links against under the hood (you won't see them without running make VERBOSE=1), one of them (but not others) should be linked against statically. Thus, by specifying
target_link_libraries(main ifcoremt.a)
in CMakeLists.txt seems to fix the problem, at least for me.

Related

cmake cross-compilation toolchain confusion

TL;DR: Why moving stuff around from the CMakeLists.txt to a dedicated toolchain file plays a role for find_package?
Trying to cross-compile with cmake and compile/link against pthread, I have the following dummy source file and CMakeLists.txt which work as expected together:
main.cc
#include <iostream>
#include <bits/c++config.h> // needed in the real world
int main()
{
std::cout << "Hello World !\n";
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(test VERSION 0.0.1)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET arm-linux-gnueabihf)
add_compile_options(-mtune=cortex-a9)
add_compile_definitions(_ARM_GCC_)
# Looking for cross compiler includes; maybe looking for rootfs-like stuff would be better
file(GLOB cross_includes LIST_DIRECTORIES true "/usr/arm-linux-gnueabihf/include/c++/*/arm-linux-gnueabihf/")
include_directories(SYSTEM ${cross_includes})
include_directories(SYSTEM /usr/arm-linux-gnueabihf/include)
find_package(Threads REQUIRED) # Not strictly necessary in this example but needed in the real world
add_executable(test main.cc)
Test:
$> rm -rf build/ && cmake -B build/ && make VERBOSE=1 -C build/
-- The C compiler identification is Clang 11.0.1
-- The CXX compiler identification is Clang 11.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/jp/sandbox/so/cmake/build
make: Entering directory '/home/jp/sandbox/so/cmake/build'
/usr/bin/cmake -S/home/jp/sandbox/so/cmake -B/home/jp/sandbox/so/cmake/build --check-build-system CMakeFiles/Makefile.cmake 0
/usr/bin/cmake -E cmake_progress_start /home/jp/sandbox/so/cmake/build/CMakeFiles /home/jp/sandbox/so/cmake/build//CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory '/home/jp/sandbox/so/cmake/build'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/depend
make[2]: Entering directory '/home/jp/sandbox/so/cmake/build'
cd /home/jp/sandbox/so/cmake/build && /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/jp/sandbox/so/cmake /home/jp/sandbox/so/cmake /home/jp/sandbox/so/cmake/build /home/jp/sandbox/so/cmake/build /home/jp/sandbox/so/cmake/build/CMakeFiles/test.dir/DependInfo.cmake --color=
Dependee "/home/jp/sandbox/so/cmake/build/CMakeFiles/test.dir/DependInfo.cmake" is newer than depender "/home/jp/sandbox/so/cmake/build/CMakeFiles/test.dir/depend.internal".
Dependee "/home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeDirectoryInformation.cmake" is newer than depender "/home/jp/sandbox/so/cmake/build/CMakeFiles/test.dir/depend.internal".
Scanning dependencies of target test
make[2]: Leaving directory '/home/jp/sandbox/so/cmake/build'
make -f CMakeFiles/test.dir/build.make CMakeFiles/test.dir/build
make[2]: Entering directory '/home/jp/sandbox/so/cmake/build'
[ 50%] Building CXX object CMakeFiles/test.dir/main.cc.o
/usr/bin/clang++ --target=arm-linux-gnueabihf -D_ARM_GCC_ -isystem /usr/arm-linux-gnueabihf/include/c++/10/arm-linux-gnueabihf -isystem /usr/arm-linux-gnueabihf/include -g -mtune=cortex-a9 -std=c++17 -o CMakeFiles/test.dir/main.cc.o -c /home/jp/sandbox/so/cmake/main.cc
[100%] Linking CXX executable test
/usr/bin/cmake -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
/usr/bin/clang++ --target=arm-linux-gnueabihf -g -rdynamic CMakeFiles/test.dir/main.cc.o -o test
make[2]: Leaving directory '/home/jp/sandbox/so/cmake/build'
[100%] Built target test
make[1]: Leaving directory '/home/jp/sandbox/so/cmake/build'
/usr/bin/cmake -E cmake_progress_start /home/jp/sandbox/so/cmake/build/CMakeFiles 0
make: Leaving directory '/home/jp/sandbox/so/cmake/build'
So far, everything works fine.
Now, the tricky part: I'd like to put the cross-compilation specific stuff in a toolchain.cmake file
toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_C_COMPILER clang)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_COMPILER_TARGET arm-linux-gnueabihf)
add_compile_options(-mtune=cortex-a9)
add_compile_definitions(_ARM_GCC_)
# Looking for cross compiler includes; maybe looking for rootfs-like stuff would be better
file(GLOB cross_includes LIST_DIRECTORIES true "/usr/arm-linux-gnueabihf/include/c++/*/arm-linux-gnueabihf/")
include_directories(SYSTEM ${cross_includes})
include_directories(SYSTEM /usr/arm-linux-gnueabihf/include)
new CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(test VERSION 0.0.1)
set(CMAKE_BUILD_TYPE Debug)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
find_package(Threads REQUIRED) # Not strictly necessary in this example but needed in the real world
add_executable(test main.cc)
Test:
$> rm -rf build/ && cmake -B build/ -DCMAKE_TOOLCHAIN_FILE=toolchain.cmake && make VERBOSE=1 -C build/
-- The C compiler identification is Clang 11.0.1
-- The CXX compiler identification is Clang 11.0.1
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working C compiler: /usr/bin/clang - skipped
-- Detecting C compile features
-- Detecting C compile features - done
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/clang++ - skipped
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Looking for pthread.h
-- Looking for pthread.h - not found
CMake Error at /usr/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:165 (message):
Could NOT find Threads (missing: Threads_FOUND)
Call Stack (most recent call first):
/usr/share/cmake-3.18/Modules/FindPackageHandleStandardArgs.cmake:458 (_FPHSA_FAILURE_MESSAGE)
/usr/share/cmake-3.18/Modules/FindThreads.cmake:234 (FIND_PACKAGE_HANDLE_STANDARD_ARGS)
CMakeLists.txt:11 (find_package)
-- Configuring incomplete, errors occurred!
See also "/home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeOutput.log".
See also "/home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeError.log".
Actually, cmake finds pthread.h but clang cannot compile it (wrong floating point abi used; stubs-hard.h should be included)
build/CMakeFiles/CMakeError.log
Determining if the include file pthread.h exists failed with the following output:
Change Dir: /home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/gmake cmTC_76e89/fast && /usr/bin/gmake -f CMakeFiles/cmTC_76e89.dir/build.make CMakeFiles/cmTC_76e89.dir/build
gmake[1]: Entering directory '/home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_76e89.dir/CheckIncludeFile.c.o
/usr/bin/clang -D_ARM_GCC_ -isystem /usr/arm-linux-gnueabihf/include/c++/10/arm-linux-gnueabihf -isystem /usr/arm-linux-gnueabihf/include/gnu -isystem /usr/arm-linux-gnueabihf/include -mtune=cortex-a9 -o CMakeFiles/cmTC_76e89.dir/CheckIncludeFile.c.o -c /home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c
In file included from /home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeTmp/CheckIncludeFile.c:1:
In file included from /usr/arm-linux-gnueabihf/include/pthread.h:21:
In file included from /usr/arm-linux-gnueabihf/include/features.h:485:
/usr/arm-linux-gnueabihf/include/gnu/stubs.h:7:11: fatal error: 'gnu/stubs-soft.h' file not found
# include <gnu/stubs-soft.h>
^~~~~~~~~~~~~~~~~~
1 error generated.
gmake[1]: *** [CMakeFiles/cmTC_76e89.dir/build.make:85: CMakeFiles/cmTC_76e89.dir/CheckIncludeFile.c.o] Error 1
gmake[1]: Leaving directory '/home/jp/sandbox/so/cmake/build/CMakeFiles/CMakeTmp'
gmake: *** [Makefile:140: cmTC_76e89/fast] Error 2
Some more Info:
I made sure the toolchain.cmake is considered (triggered a parse error to check)
$> cat toolchain.cmake CMakeLists.txt
produces the same output as the "original" CMakeLists.txt
pthread.h can be found on my System at the following locations:
$> find /usr/ -name pthread.h
/usr/arm-linux-gnueabi/include/pthread.h
/usr/arm-linux-gnueabihf/include/pthread.h
/usr/include/newlib/pthread.h /usr/include/pthread.h
As #Tsyvarev pointed out, the CMakeError.log contained useful information (the question was edited with its content).
The issue:
cmake finds pthread.h but clang cannot compile it
...
/usr/arm-linux-gnueabihf/include/gnu/stubs.h:7:11: fatal error: 'gnu/stubs-soft.h' file not found
...
The clang invocation is interesting because of "/usr/bin/clang" (expected clang++) and no -target flag used
The solution:
Tell cmake to also use a specific -target flag for C cross-compilers supoorting it:
Add the following in target.cmake
set(CMAKE_C_COMPILER_TARGET arm-linux-gnueabihf)
(Same target as CMAKE_CXX_COMPILER_TARGET)

why 'target_link_libraries' command is not working? (cplex)

I tried to compile an project with cmake command(all implementation is written by other people. my job is just compile and run.)
error messages after cmake
-- The C compiler identification is GNU 9.3.0
-- The CXX compiler identification is GNU 9.3.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
fatal: ambiguous argument 'HEAD': unknown revision or path not in the working tree.
Use '--' to separate paths from revisions, like this:
'git <command> [<revision>...] -- [<file>...]'
fatal: bad revision 'HEAD'
-- Looking for pthread.h
-- Looking for pthread.h - found
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD
-- Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Found LibXml2: /usr/lib/x86_64-linux-gnu/libxml2.so (found version "2.9.10")
-- Found ZLIB: /usr/lib/x86_64-linux-gnu/libz.so (found version "1.2.11")
-- CPLEX Library: /opt/ibm/ILOG/CPLEX_Studio1210/cplex/lib/x86-64_linux/static_pic/libcplex.a
-- ILOCPLEX Library: /opt/ibm/ILOG/CPLEX_Studio1210/cplex/lib/x86-64_linux/static_pic/libilocplex.a
-- CONCERT Library: /opt/ibm/ILOG/CPLEX_Studio1210/concert/lib/x86-64_linux/static_pic/libconcert.a
-- CPLEX Bin Dir: /opt/ibm/ILOG/CPLEX_Studio1210/cplex/bin/x86-64_linux
-- Found CPLEX: /opt/ibm/ILOG/CPLEX_Studio1210/cplex/lib/x86-64_linux/static_pic/libcplex.a
-- Configuring done
CMake Error at cpxutils/CMakeLists.txt:25 (add_executable):
Target "cpx_solver" links to target "Cplex::Cplex" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
CMake Error at cpxutils/CMakeLists.txt:13 (add_library):
Target "cpxutils" links to target "Cplex::Cplex" but the target was not
found. Perhaps a find_package() call is missing for an IMPORTED target, or
an ALIAS target is missing?
CMake Error at feaspump/CMakeLists.txt:17 (add_executable):
Target "fp2" links to target "Cplex::Cplex" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
CMake Error at feaspump/CMakeLists.txt:5 (add_library):
Target "fp" links to target "Cplex::Cplex" but the target was not found.
Perhaps a find_package() call is missing for an IMPORTED target, or an
ALIAS target is missing?
-- Generating done
CMake Generate step failed. Build files cannot be regenerated correctly.
and this is cpxutils/CMakeLists.txt file.
cmake_minimum_required(VERSION 3.6)
# Find CPLEX library
find_package(CPLEX)
# Export CPLEX_FOUND for CMakeLists.txt files in other subdirectories
set(CPLEX_FOUND ${CPLEX_FOUND} PARENT_SCOPE)
if (CPLEX_FOUND)
# Define libcpxutils
add_library(cpxutils STATIC cpxutils.cpp cpxmacro.cpp model.cpp gomory.cpp cpxapp.cpp)
target_include_directories(cpxutils PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(cpxutils PUBLIC utils Cplex::Cplex)
add_library(Cpxutils::Lib ALIAS cpxutils)
# Define cpx_solver executable
add_executable(cpx_solver EXCLUDE_FROM_ALL cpx_solver.cpp)
target_include_directories(cpx_solver PUBLIC
$<BUILD_INTERFACE:${CMAKE_SOURCE_DIR}>
$<INSTALL_INTERFACE:include>
)
target_link_libraries(cpx_solver PUBLIC utils Cpxutils::Lib)
else()
message(WARNING "Disabling CPXUTILS subproject")
endif()
i don't know why cannot find target despite CPLEX package is found.
i'm using WSL1 (Ubuntu) environment.
↑ problem solved. thanks for #Tsyvarev.
=== additional problem ===
Even though cmake commad works without any error, I cant find execution file. So I checked 'CMakeError.log' file then, there are some error.
This is CMakeError.log file.
Performing C SOURCE FILE Test CMAKE_HAVE_LIBC_PTHREAD failed with the following output:
Change Dir: /mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make cmTC_92254/fast && /usr/bin/make -f CMakeFiles/cmTC_92254.dir/build.make CMakeFiles/cmTC_92254.dir/build
make[1]: Entering directory '/mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_92254.dir/src.c.o
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD -o CMakeFiles/cmTC_92254.dir/src.c.o -c /mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp/src.c
Linking C executable cmTC_92254
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_92254.dir/link.txt --verbose=1
/usr/bin/cc -DCMAKE_HAVE_LIBC_PTHREAD CMakeFiles/cmTC_92254.dir/src.c.o -o cmTC_92254
/usr/bin/ld: CMakeFiles/cmTC_92254.dir/src.c.o: in function `main':
src.c:(.text+0x46): undefined reference to `pthread_create'
/usr/bin/ld: src.c:(.text+0x52): undefined reference to `pthread_detach'
/usr/bin/ld: src.c:(.text+0x63): undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_92254.dir/build.make:87: cmTC_92254] Error 1
make[1]: Leaving directory '/mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_92254/fast] Error 2
Source file was:
#include <pthread.h>
void* test_func(void* data)
{
return data;
}
int main(void)
{
pthread_t thread;
pthread_create(&thread, NULL, test_func, NULL);
pthread_detach(thread);
pthread_join(thread, NULL);
pthread_atfork(NULL, NULL, NULL);
pthread_exit(NULL);
return 0;
}
Determining if the function pthread_create exists in the pthreads failed with the following output:
Change Dir: /mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp
Run Build Command(s):/usr/bin/make cmTC_8ae2b/fast && /usr/bin/make -f CMakeFiles/cmTC_8ae2b.dir/build.make CMakeFiles/cmTC_8ae2b.dir/build
make[1]: Entering directory '/mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp'
Building C object CMakeFiles/cmTC_8ae2b.dir/CheckFunctionExists.c.o
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create -o CMakeFiles/cmTC_8ae2b.dir/CheckFunctionExists.c.o -c /usr/share/cmake-3.16/Modules/CheckFunctionExists.c
Linking C executable cmTC_8ae2b
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_8ae2b.dir/link.txt --verbose=1
/usr/bin/cc -DCHECK_FUNCTION_EXISTS=pthread_create CMakeFiles/cmTC_8ae2b.dir/CheckFunctionExists.c.o -o cmTC_8ae2b -lpthreads
/usr/bin/ld: cannot find -lpthreads
collect2: error: ld returned 1 exit status
make[1]: *** [CMakeFiles/cmTC_8ae2b.dir/build.make:87: cmTC_8ae2b] Error 1
make[1]: Leaving directory '/mnt/c/Users/aero5010/Desktop/CBC+FP/CBC+FP/fp2/build/CMakeFiles/CMakeTmp'
make: *** [Makefile:121: cmTC_8ae2b/fast] Error 2
I think this is why i cant find execution file. But I dont know why this error occured despite pthread library is installed well...
** thank you for every advice about Stackoverflow Manners.

cmake: c++: error: ${CFLAGS}: No such file or directory

I'm trying to follow this cmake tutorial. But when it comes to calling cmake for the first time I get the following error:
cmake -G "Unix Makefiles" .. --debug-trycompile
debug trycompile on
-- The C compiler identification is GNU 6.3.1
-- The CXX compiler identification is unknown
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- broken
CMake Error at /usr/share/cmake-3.7/Modules/CMakeTestCXXCompiler.cmake:44 (message):
The C++ compiler "/usr/bin/c++" is not able to compile a simple test
program.
It fails with the following output:
Change Dir: /home/user01/code/tests/cmake_tutorial_02/build/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_7a86e/fast"
/usr/bin/make -f CMakeFiles/cmTC_7a86e.dir/build.make
CMakeFiles/cmTC_7a86e.dir/build
make[1]: Entering directory
'/home/user01/code/tests/cmake_tutorial_02/build/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_7a86e.dir/testCXXCompiler.cxx.o
/usr/bin/c++ -march=native -O2 -pipe -fstack-protector-strong -o
CMakeFiles/cmTC_7a86e.dir/testCXXCompiler.cxx.o -c
/home/user01/code/tests/cmake_tutorial_02/build/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTC_7a86e
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7a86e.dir/link.txt
--verbose=1
/usr/bin/c++ ${CFLAGS} CMakeFiles/cmTC_7a86e.dir/testCXXCompiler.cxx.o -o
cmTC_7a86e
c++: error: ${CFLAGS}: No such file or directory
When I call echo $CFLAGS in bash the value is:
-march=native -O2 -pipe -fstack-protector-strong
But it looks like ${CFLAGS} is not defined while executing cmake_link_script. To verify I changed ${CFLAGS} in link.txt to -march=native -O2 -pipe -fstack-protector-strong and manually called:
/usr/bin/cmake -E cmake_link_script CMakeFiles/cmTC_7a86e.dir/link.txt
which works.
Next I tried adding the following line to CMakeLists.txt:
set (CFLAGS -O2)
which didn't help.
Neither did calling:
cmake -DCFLAGS=-O2 -G "Unix Makefiles" ..
So my question is, how to define CFLAGS so it is defined when calling the cmake_link_script in cmake's command mode?
EDIT
It looks like cmake fails withe the "Unix Makefiles" generator and doesn't even get to the code from the Tutorial.
the CMakeLists.txt just looks like this:
project("To Do List")
add_executable(toDo main.cc
ToDo.cc)
EDIT2
I found a workaround.
For some reason the generator sets CMAKE_CXX_FLAGS:STRING='${CFLAGS} ' in CMakeCache.txt.
But when the generator gets invoked with:
cmake -DCMAKE_CXX_FLAGS:STRING="${CFLAGS} " -G "Unix Makefiles" ..
everything works. Even the "The CXX compiler identification is unknown" message is gone.
However, I would still like to know why the generator failed before?

cmake unable to find libstdc++

(Using elementaryOS/Ubuntu)
I'm cross-compiling x265 and I encouraged annoying problem. For some reason cmake doesn't want to accept -static-libstdc++, because ld is apparently unable to find it. Cmake is built from Source Code, working pretty well without -static-libstdc++, if I then copy libstdc++-6.dll from mingw-w64 libs to folder with x265.exe, it works well, but I want it to build with it. mingw was built with this script and contains these libraries, but I don't know where is ld looking for them.
Here is output of cmake attempt:
-- cmake version 3.5.0-rc1
-- The C compiler identification is GNU 5.2.0
-- The CXX compiler identification is GNU 5.2.0
-- Check for working C compiler: /home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-gcc
-- Check for working C compiler: /home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-g++
-- Check for working CXX compiler: /home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-g++ -- broken
CMake Error at /usr/local/share/cmake-3.5/Modules/CMakeTestCXXCompiler.cmake:54 (message):
The C++ compiler
"/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-g++" is not
able to compile a simple test program.
It fails with the following output:
Change Dir: /home/myname/x265/build/linux/12bit/CMakeFiles/CMakeTmp
Run Build Command:"/usr/bin/make" "cmTC_e7611/fast"
/usr/bin/make -f CMakeFiles/cmTC_e7611.dir/build.make
CMakeFiles/cmTC_e7611.dir/build
make[1]: Entering directory
`/home/myname/x265/build/linux/12bit/CMakeFiles/CMakeTmp'
Building CXX object CMakeFiles/cmTC_e7611.dir/testCXXCompiler.cxx.obj
/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-g++
-static-libgcc -static-libstdc++ -o
CMakeFiles/cmTC_e7611.dir/testCXXCompiler.cxx.obj -c
/home/myname/x265/build/linux/12bit/CMakeFiles/CMakeTmp/testCXXCompiler.cxx
Linking CXX executable cmTC_e7611.exe
/usr/local/bin/cmake -E cmake_link_script
CMakeFiles/cmTC_e7611.dir/link.txt --verbose=1
/usr/local/bin/cmake -E remove -f CMakeFiles/cmTC_e7611.dir/objects.a
/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-ar cr
CMakeFiles/cmTC_e7611.dir/objects.a #CMakeFiles/cmTC_e7611.dir/objects1.rsp
/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-g++
-static-libgcc -static-libstdc++ -Wl,--whole-archive
CMakeFiles/cmTC_e7611.dir/objects.a -Wl,--no-whole-archive -o
cmTC_e7611.exe -Wl,--out-implib,libcmTC_e7611.dll.a
-Wl,--major-image-version,0,--minor-image-version,0
#CMakeFiles/cmTC_e7611.dir/linklibs.rsp
/home/myname/mingw-w64/mingw-w64-x86_64/lib/gcc/x86_64-w64-mingw32/5.2.0/../../../../x86_64-w64-mingw32/bin/ld:
cannot find -lstdc++
collect2: error: ld returned 1 exit status
make[1]: *** [cmTC_e7611.exe] Error 1
make[1]: Leaving directory
`/home/myname/x265/build/linux/12bit/CMakeFiles/CMakeTmp'
make: *** [cmTC_e7611/fast] Error 2
CMake will not be able to correctly generate this project.
Call Stack (most recent call first):
CMakeLists.txt:19 (project)
-- Configuring incomplete, errors occurred!
See also "/home/myname/x265/build/linux/12bit/CMakeFiles/CMakeOutput.log".
See also "/home/myname/x265/build/linux/12bit/CMakeFiles/CMakeError.log".
make: *** No targets specified and no makefile found. Stop.
Cmake is run like code below.
cmake -DCMAKE_C_COMPILER='/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-gcc' -DCMAKE_CXX_COMPILER='/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-g++' -DCMAKE_RC_COMPILER='/home/myname/mingw-w64/mingw-w64-x86_64/bin/x86_64-w64-mingw32-windres' -DCMAKE_CXX_FLAGS="-static-libgcc -static-libstdc++" -DCMAKE_C_FLAGS="-static-libgcc -static-libstdc++" -DCMAKE_TOOLCHAIN_FILE='/home/myname/x265/build/linux/build.cmake' ../../../source -DHIGH_BIT_DEPTH=ON -DEXPORT_C_API=OFF -DENABLE_SHARED=OFF -DENABLE_CLI=OFF -DMAIN12=ON
make ${MAKEFLAGS}
build.cmake file is not much important here, but it contains:
SET(CMAKE_SYSTEM_NAME Windows)
SET(CMAKE_ASM_YASM_COMPILER yasm)
Any ideas how to make -static-libstdc++ work? I tried linking it in many folders, but I couldn't find out where is ld looking for it.
I managed to solve this. Source of my confusion was the fact that you can use -static-libgcc, so I thought even -static-libstdc++ should work. The problem was that there indeed were libraries, but not the .a files for libstdc++. These can be disabled using this parameter when using the script. Then both -static-lib options work well.
bash ./mingw-w64-build-3.6.7 --disable-shared

Error cross-compiling cpp-netlib for Raspbian

I've successfully compiled boost using the cross compiler (used the instructions from Installing Raspberry Pi Cross-Compiler) and it worked just fine.
I moved the lib and the include into the rootfs so that cmake finds it... and when I run cmake, I get
gervasio#ubuntu:~/cpp-netlib-0.10.1$ cmake -DCMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake .
-- The C compiler identification is GNU
-- The CXX compiler identification is GNU
-- Check for working C compiler: /home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc
-- Check for working C compiler: /home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++
-- Check for working CXX compiler: /home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Boost version: 1.55.0
-- Found the following Boost libraries:
-- unit_test_framework
-- system
-- regex
-- date_time
-- thread
-- filesystem
-- program_options
-- chrono
-- Found OpenSSL: /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libssl.so;/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so (found version "1..1")
-- Looking for include files CMAKE_HAVE_PTHREAD_H
-- Looking for include files CMAKE_HAVE_PTHREAD_H - found
-- Looking for pthread_create in pthreads
-- Looking for pthread_create in pthreads - not found
-- Looking for pthread_create in pthread
-- Looking for pthread_create in pthread - found
-- Found Threads: TRUE
-- Configuring done
-- Generating done
-- Build files have been written to: /home/gervasio/cpp-netlib-0.10.1
which is awesome... now, when I run make, it ends with
gervasio#ubuntu:~/cpp-netlib-0.10.1$ make
...
[ 15%] Building CXX object libs/network/test/CMakeFiles/cpp-netlib-message_test.dir/message_test.cpp.o
cd /home/gervasio/cpp-netlib-0.10.1/libs/network/test && /home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ -DBOOST_NETWORK_ENABLE_HTTPS -Wall -I/home/gervasio/raspberrypi/rootfs/usr/include -I/home/gervasio/cpp-netlib-0.10.1 -Wall -o CMakeFiles/cpp-netlib-message_test.dir/message_test.cpp.o -c /home/gervasio/cpp-netlib-0.10.1/libs/network/test/message_test.cpp
Linking CXX executable ../../../tests/cpp-netlib-message_test
cd /home/gervasio/cpp-netlib-0.10.1/libs/network/test && /usr/bin/cmake -E cmake_link_script CMakeFiles/cpp-netlib-message_test.dir/link.txt --verbose=1
/home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ -Wall CMakeFiles/cpp-netlib-message_test.dir/message_test.cpp.o -o ../../../tests/cpp-netlib-message_test -rdynamic /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_unit_test_framework.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_system.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_regex.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_date_time.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_thread.a -lpthread /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_filesystem.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_program_options.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_chrono.a -lpthread ../src/libcppnetlib-uri.a /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libssl.so /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so -lrt -Wl,-rpath,/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf
/home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld: warning: libz.so.1, needed by /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libssl.so, not found (try using -rpath or -rpath-link)
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `deflateInit_'
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `deflate'
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `deflateEnd'
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `inflate'
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `inflateInit_'
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `inflateEnd'
/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so: undefined reference to `zError'
collect2: error: ld returned 1 exit status
make[2]: *** [tests/cpp-netlib-message_test] Error 1
make[2]: Leaving directory `/home/gervasio/cpp-netlib-0.10.1'
make[1]: *** [libs/network/test/CMakeFiles/cpp-netlib-message_test.dir/all] Error 2
make[1]: Leaving directory `/home/gervasio/cpp-netlib-0.10.1'
make: *** [all] Error 2
but... inside ~/raspberrypi/rootfs/lib/arm-linux-gnueabihf there's a libz.so.1
gervasio#ubuntu:~/raspberrypi/rootfs/lib/arm-linux-gnueabihf$ ls -lah libz.so*
lrwxrwxrwx 1 gervasio gervasio 13 Jun 24 2012 libz.so.1 -> libz.so.1.2.7
-rw-r--r-- 1 gervasio gervasio 86K Jun 24 2012 libz.so.1.2.7
I know it's failing just on the test, and that I can use the libraries... but I'd really like to have a good environment to cross compile whatever I need.
Thanks for your help :)
edit: just ran ldd libssl.so on the raspberry and got
/usr/lib/arm-linux-gnueabihf/libcofi_rpi.so (0xb6ec6000)
libcrypto.so.1.0.0 => /usr/lib/arm-linux-gnueabihf/libcrypto.so.1.0.0 (0xb6d57000)
libdl.so.2 => /lib/arm-linux-gnueabihf/libdl.so.2 (0xb6d4c000)
libz.so.1 => /lib/arm-linux-gnueabihf/libz.so.1 (0xb6d2e000)
libc.so.6 => /lib/arm-linux-gnueabihf/libc.so.6 (0xb6bff000)
/lib/ld-linux-armhf.so.3 (0xb6f24000)
libgcc_s.so.1 => /lib/arm-linux-gnueabihf/libgcc_s.so.1 (0xb6bd7000)
edit 2: If I run cmake with -DCMAKE_SKIP_BUILD_RPATH=FALSE -DCMAKE_BUILD_WITH_INSTALL_RPATH=FALSE -DCMAKE_INSTALL_RPATH=/home/gervasio/raspberrypi/rootfs/lib/arm-linux-gnueabihf -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE I get this output
/home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/arm-linux-gnueabihf-g++ -Wall CMakeFiles/cpp-netlib-message_test.dir/message_test.cpp.o -o ../../../tests/cpp-netlib-message_test -rdynamic /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_unit_test_framework.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_system.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_regex.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_date_time.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_thread.a -lpthread /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_filesystem.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_program_options.a /home/gervasio/raspberrypi/rootfs/usr/lib/libboost_chrono.a -lpthread ../src/libcppnetlib-uri.a /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libssl.so /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libcrypto.so -lrt -Wl,-rpath,/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf
/home/gervasio/raspberrypi/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin/../lib/gcc/arm-linux-gnueabihf/4.7.2/../../../../arm-linux-gnueabihf/bin/ld: warning: libz.so.1, needed by /home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf/libssl.so, not found (try using -rpath or -rpath-link)
where it says -rpath,/home/gervasio/raspberrypi/rootfs/usr/lib/arm-linux-gnueabihf that doesn't seem ok, right?
Thanks to #ruslo and that link he posted, I figured it out... this is the way to tell cmake to use rpath
cmake -DCMAKE_TOOLCHAIN_FILE=$HOME/raspberrypi/pi.cmake -DCMAKE_SKIP_BUILD_RPATH=FALSE -DCMAKE_BUILD_WITH_INSTALL_RPATH=TRUE -DCMAKE_INSTALL_RPATH=/home/gervasio/raspberrypi/rootfs/lib/arm-linux-gnueabihf -DCMAKE_INSTALL_RPATH_USE_LINK_PATH=TRUE .