I am trying to achieve the equivalent of following command by CMake.
gcc -static -O0 -g main.c /usr/lib/x86_64-linux-gnu/libpthread.a
My cmake script successfully built executable file, but gave segmentation fault at run.
Can anybody point me what should I fix ? The command line above built and ran normally, and if I linked libpthtread.so by my cmake script it also terminated without issue.
-
main.c
#include <pthread.h>
#include <stdio.h>void work(void parg){
int val = (int)parg;
printf(“worker tid=%d\n”, *val);
*val = 100;
}int main (){
pthread_t handle;
int data = 0;pthread_create(&handle, NULL, work, &data); pthread_join(handle, NULL); printf("final = %d\n", data); return 0;
}
-
CMakeLists.txt
cmake_minimum_required(VERSION 3.13 FATAL_ERROR)
project(pthread_task)
add_executable(pthread_task main.c)
target_link_libraries(pthread_task pthread.a libc.a)
install(TARGETS pthread_task DESTINATION ${CMAKE_INSTALL_PREFIX}) -
cmake command
mkdir build
cd build
cmake -DCMAKE_BUILD_TYPE=“DEBUG” -DCMAKE_C_FLAGS_DEBUG="-O0 -g -static" …/ -
env
$ dpkg -l | grep cmake
ii cmake 3.13.4-1 amd64 cross-platform, open-source make system$ uname -a
Linux x250 4.19.0-9-amd64 #1 SMP Debian 4.19.118-2+deb10u1 (2020-06-07) x86_64 GNU/Linux