程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何在 CMakeLists Clion 中为 shm_open 添加编译器标志大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何在 CMakeLists Clion 中为 shm_open 添加编译器标志?

开发过程中遇到如何在 CMakeLists Clion 中为 shm_open 添加编译器标志的问题如何解决?下面主要结合日常开发的经验,给出你关于如何在 CMakeLists Clion 中为 shm_open 添加编译器标志的解决方法建议,希望对你解决如何在 CMakeLists Clion 中为 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)

解决方法

要将目标与库链接,请使用 target_link_libraries。你会:

add_executable(Share_Memory_Project main.C)
target_link_libraries(Share_Memory_Project PUBLIC rt)

大佬总结

以上是大佬教程为你收集整理的如何在 CMakeLists Clion 中为 shm_open 添加编译器标志全部内容,希望文章能够帮你解决如何在 CMakeLists Clion 中为 shm_open 添加编译器标志所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。