Intelligent
V2EX  ›  问与答

请教一个 pthread 的问题,谢谢

  •  
  •   Intelligent · Apr 5, 2020 · 1157 views
    This topic created in 2227 days ago, the information mentioned may be changed or developed.
    在 linux 里面用 pthread 写了一段代码,中间用了信号:sem_init(&sem, 0, 1)

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

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

    但是我以前写 pthread 好像也没用到这个,是不是用到了信号才需要。
    codehz
        3
    codehz  
       Apr 5, 2020 via Android
    (现代的写法不是写死 pthread,而是用
    find_package(Threads REQUIRED)
    然后加 Threads::Threads 的链接
    这样就可以尽可能多的兼容不同系统
    因为理论上只有用了 glibc 的 linux 才需要链接 pthread
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   995 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:47 · PVG 03:47 · LAX 12:47 · JFK 15:47
    ♥ Do have faith in what you're doing.