如何在 shm_open 的 CMakeLists Clion 中添加编译器标志
How to add Compiler Flag in CMakeLists Clion for shm_open
简单程序:
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
int main() {
int shm_fd;
shm_fd=shm_open("sh",O_CREAT|O_RDWR,0666);
return 0;
}
错误是:
Warnung: undefined reference to »shm_open«
collect2: error: ld returned 1 exit status
CMakeFiles/Share_Memory_Project.dir/build.make:102: recipe for target 'Share_Memory_Project' failed
make[3]: *** [Share_Memory_Project] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/Share_Memory_Project.dir/all' failed
make[2]: *** [CMakeFiles/Share_Memory_Project.dir/all] Error 2
CMakeFiles/Makefile2:101: recipe for target 'CMakeFiles/Share_Memory_Project.dir/rule' failed
make[1]: *** [CMakeFiles/Share_Memory_Project.dir/rule] Error 2
Makefile:137: recipe for target 'Share_Memory_Project' failed
make: *** [Share_Memory_Project] Error 2
要解决这个问题,我必须添加一个编译器标志:
-lrt
最后。像这样:gcc main.c -o main -lrt
。所以当我把它放在Ubuntu终端的命令行中时,它编译得很好。
我的问题是:如何在 Clion 的 CMakeLists.txt 中添加此标志?
我的文件目前的样子:
cmake_minimum_required(VERSION 3.19)
project(Share_Memory_Project C)
set(CMAKE_C_STANDARD 99)
add_executable(Share_Memory_Project main.c)
要 link 带有库的目标,请使用 target_link_libraries
。你会:
add_executable(Share_Memory_Project main.c)
target_link_libraries(Share_Memory_Project PUBLIC rt)
简单程序:
#include <stdlib.h>
#include <sys/mman.h>
#include <fcntl.h>
int main() {
int shm_fd;
shm_fd=shm_open("sh",O_CREAT|O_RDWR,0666);
return 0;
}
错误是:
Warnung: undefined reference to »shm_open«
collect2: error: ld returned 1 exit status
CMakeFiles/Share_Memory_Project.dir/build.make:102: recipe for target 'Share_Memory_Project' failed
make[3]: *** [Share_Memory_Project] Error 1
CMakeFiles/Makefile2:94: recipe for target 'CMakeFiles/Share_Memory_Project.dir/all' failed
make[2]: *** [CMakeFiles/Share_Memory_Project.dir/all] Error 2
CMakeFiles/Makefile2:101: recipe for target 'CMakeFiles/Share_Memory_Project.dir/rule' failed
make[1]: *** [CMakeFiles/Share_Memory_Project.dir/rule] Error 2
Makefile:137: recipe for target 'Share_Memory_Project' failed
make: *** [Share_Memory_Project] Error 2
要解决这个问题,我必须添加一个编译器标志:
-lrt
最后。像这样:gcc main.c -o main -lrt
。所以当我把它放在Ubuntu终端的命令行中时,它编译得很好。
我的问题是:如何在 Clion 的 CMakeLists.txt 中添加此标志?
我的文件目前的样子:
cmake_minimum_required(VERSION 3.19)
project(Share_Memory_Project C)
set(CMAKE_C_STANDARD 99)
add_executable(Share_Memory_Project main.c)
要 link 带有库的目标,请使用 target_link_libraries
。你会:
add_executable(Share_Memory_Project main.c)
target_link_libraries(Share_Memory_Project PUBLIC rt)