Results 1 to 5 of 5

Thread: How do I include -lpthread (or other g++ args) in a CMake project?

  1. #1
    Join Date
    Nov 2013
    Location
    NB, Canada
    Posts
    1
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default How do I include -lpthread (or other g++ args) in a CMake project?

    Earlier today I was trying to get my project to compile with the g++ option, "-std=c++11". Being new to both Qt Creator & CMake, I thought that the place to put this option was under Tools -> Options -> Build & Run -> Compilers -> Platform codegen flags. I quickly found out that this isn't where it goes, and after some searching, learned that I should adding this to my CMakeLists.txt: "ADD_DEFINITIONS( -std=c++11 )". Good enough.

    Now, I'm trying to add the g++ option, "-lpthread". This time, I tried adding in CMakeLists.txt like this: "ADD_DEFINITIONS( -std=c++11 -lpthread)". This didn't do the trick, so I tried adding "-pthread" in Tools -> Options -> Build & Run -> Compilers -> Platform linker flags. This didn't work for me either.

    Where should I be trying to add this option?

    Thanks.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,552
    Thanks
    13
    Thanked 1,579 Times in 1,508 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How do I include -lpthread (or other g++ args) in a CMake project?

    Seems to me you want:
    Qt Code:
    1. target_link_libraries(<targetname> lib1 lib2 ...)
    2.  
    3. e.g.
    4. target_link_libraries( MyCoolApp pthread )
    To copy to clipboard, switch view to plain text mode 
    You should check the CMake Qt support is not already adding this as qmake does for a build on Linux.

  3. #3
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How do I include -lpthread (or other g++ args) in a CMake project?

    Also, a better solution for enabling C++11 on gcc under CMake is the following:
    Qt Code:
    1. if(CMAKE_COMPILER_IS_GNUCC)
    2. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
    3. endif(CMAKE_COMPILER_IS_GNUCC)
    To copy to clipboard, switch view to plain text mode 
    On a fun note, I also tend to add the following to all my CMake projects:
    Qt Code:
    1. option(enable_maintainer "Enables the maintainer CFLAGS (-Wall -Werror)" OFF)
    2. if(enable_maintainer)
    3. if(CMAKE_COMPILER_IS_GNUCXX)
    4. set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Werror")
    5. endif(CMAKE_COMPILER_IS_GNUCXX)
    6. endif(enable_maintainer)
    To copy to clipboard, switch view to plain text mode 
    ... which essentially means that any compiler warnings will show up as errors and demand to be fixed before things will actually compile... but it's turned off by default, so people don't accidentally turn it on.
    Fully concur with ChrisW67's advice on target_link_libraries command for including the pthread library. He's right, though - if your project is using Qt at all (not clear from your post), then the pthread library is likely already being pulled in as a dependency of QtCore, and you shouldn't need to pull it in with any further calls in CMakeLists.txt.
    Life without passion is death in disguise

  4. #4
    Join Date
    Mar 2019
    Posts
    1

    Default Re: How do I include -lpthread (or other g++ args) in a CMake project?

    I am having the same problem, do you have a solution if so please share!
    Thank you in advance!

  5. #5
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Thanks
    24
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android

    Default Re: How do I include -lpthread (or other g++ args) in a CMake project?

    Which problem, specifically? Including pthreads support or including C++11 support? pthreads is discussed above... I'd recommend against following the above (outdated) advice for C++11, though... cmake has since integrated C++11 (and later) compiler features into the build tool so it's not tied to a specific compiler - it should check for the features you're using for any given compiler.
    Life without passion is death in disguise

Similar Threads

  1. Qt Creator Adding files to cmake project
    By xadian in forum Qt Tools
    Replies: 1
    Last Post: 1st April 2013, 15:18
  2. include cmake inside qmake
    By shenakan in forum Qt Programming
    Replies: 1
    Last Post: 23rd May 2011, 15:15
  3. How to convert QT Project to CMake project?
    By Kevin Hoang in forum Qt Programming
    Replies: 2
    Last Post: 29th March 2011, 09:48
  4. Openning a qt project using cmake
    By Hogwarts in forum Newbie
    Replies: 1
    Last Post: 23rd February 2011, 18:48
  5. build project with cmake does not work
    By codebehind in forum Qt Programming
    Replies: 4
    Last Post: 18th August 2008, 12:29

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.