V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX 提问指南
Intelligent
V2EX  ›  问与答

请教一个 pthread 的问题,谢谢

  •  
  •   Intelligent · 2020-04-05 12:39:55 +08:00 · 738 次点击
    这是一个创建于 1474 天前的主题,其中的信息可能已经有所发展或是发生改变。
    在 linux 里面用 pthread 写了一段代码,中间用了信号:sem_init(&sem, 0, 1)

    发现在 clion 中无法正常 run,需要在 cmake 里面添加:target_link_libraries(${PROJECT_NAME} pthread)

    才能正常 run,请问这句话是什么?为什么一定要写这句话才能在 clion 里面 run 呢?但是在控制台就不需要,直接 lptherad 就行了
    BrettD
        1
    BrettD  
       2020-04-05 12:45:25 +08:00 via iPhone
    这行 cmake 意思就是链接 pthread
    Intelligent
        2
    Intelligent  
    OP
       2020-04-05 12:53:36 +08:00
    @BrettD 谢谢,原来是这样。

    但是我以前写 pthread 好像也没用到这个,是不是用到了信号才需要。
    codehz
        3
    codehz  
       2020-04-05 13:10:28 +08:00 via Android
    (现代的写法不是写死 pthread,而是用
    find_package(Threads REQUIRED)
    然后加 Threads::Threads 的链接
    这样就可以尽可能多的兼容不同系统
    因为理论上只有用了 glibc 的 linux 才需要链接 pthread
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1006 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:27 · PVG 06:27 · LAX 15:27 · JFK 18:27
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.