title: Link library correct with ROS and cmake
body: <p>I'm trying to add use some code for a MOXA I/O Ethernet module with ROS. I'm using some example code, to ensure it works. I have compiled the code with gcc, so I know the code works. I compiled it from a terminal with this line:</p>
  
  <blockquote>
    <p>g++ -L/usr/lib/x86_64-linux-gnu -pthread main.cpp -Wall -O3 -omain_test -L/usr/local/lib -lmxio_x64    </p>
  </blockquote>
  
  <p>I get the main_test.out and it works.</p>
  
  <p>So I created a catkin workspace and a new package by following the ROS tutorial. I add my code to the src folder, edits the CMakeList and the package.xml. Then when I try to run catkin_make -Wall (to get rid of a lot of warnings), I get the following message:</p>
  
  <pre><code>Base path: /home/johau/ros_ws
  Source space: /home/johau/ros_ws/src
  Build space: /home/johau/ros_ws/build
  Devel space: /home/johau/ros_ws/devel
  Install space: /home/johau/ros_ws/install
  ####
  #### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
  ####
  -- Using CATKIN_DEVEL_PREFIX: /home/johau/ros_ws/devel
  -- Using CMAKE_PREFIX_PATH: /home/johau/ros_ws/devel;/opt/ros/indigo
  -- This workspace overlays: /home/johau/ros_ws/devel;/opt/ros/indigo
  -- Using PYTHON_EXECUTABLE: /usr/bin/python
  -- Using Debian Python package layout
  -- Using empy: /usr/bin/empy
  -- Using CATKIN_ENABLE_TESTING: ON
  -- Call enable_testing()
  -- Using CATKIN_TEST_RESULTS_DIR: /home/johau/ros_ws/build/test_results
  -- Found gtest sources under '/usr/src/gtest': gtests will be built
  -- Using Python nosetests: /usr/bin/nosetests-2.7
  -- catkin 0.6.9
  -- BUILD_SHARED_LIBS is on
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- ~~  traversing 1 packages in topological order:
  -- ~~  - master
  -- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  -- +++ processing catkin package: 'master'
  -- ==&gt; add_subdirectory(master)
  -- Boost version: 1.54.0
  -- Found the following Boost libraries:
  --   thread
  -- Configuring done
  -- Generating done
  -- Build files have been written to: /home/johau/ros_ws/build
  ####
  #### Running command: "make -Wall -j8 -l8" in "/home/johau/ros_ws/build"
  ####
  Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
  CMakeFiles/main.dir/src/main.cpp.o: In function `main':
  main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
  /usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
  collect2: error: ld returned 1 exit status
  make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
  make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
  make: *** [all] Error 2
  Invoking "make" failed
  </code></pre>
  
  <p>I also got the "undefined reference to 'phread_create'" when I compiled with gcc, and solved that problem by calling -pthread before main.cpp.
  So my problem now is, how can I do the same with CMake?
  I have searched a lot and tried different solutions, but nothing have worked so far. I'm pretty green, when it comes to ROS and CMake, so I'm not sure what to add where, or if I have written something wrong somewhere in my CMakeLists.txt or package.xml. </p>
  
  <p>I'm using Ubuntu 14.04 LTS and ROS Indigo.
  If you need anymore info, let my know.</p>
  
  <p>On before hand, thanks.</p>
  
  <p>[EDIT 1]
  I have now tried adding</p>
  
  <blockquote>
    <p>set(CMAKE_CXX_FLAGS "-lpthread")</p>
  </blockquote>
  
  <p>After project(master) in the CMakeLists.txt.
  I also tried with:</p>
  
  <blockquote>
    <p>set(CMAKE_CXX_FLAGS "-L/usr/lib/x86_64-linux-gnu -pthread")</p>
  </blockquote>
  
  <p>and</p>
  
  <pre><code>set(CMAKE_CXX_FLAGS "-L/usr/lib/x86_64-linux-gnu")
  set(CMAKE_CXX_FLAGS "-pthread")
  </code></pre>
  
  <p>But it gave the same error message.</p>
  
  <p>My CMakeLists.txt:</p>
  
  <pre><code>cmake_minimum_required(VERSION 2.8.3)
  project(master)
  
  unset(MOXA_LIBRARY CACHE)
  
  find_library(
     MOXA_LIBRARY
     NAMES mxio_x64
     PATHS /usr/local/lib
     PATH_SUFFIXES lib
     NO_DEFAULT_PATH
  )
  
  if(MOXA_LIBRARY STREQUAL "MOXA_LIBRARY-NOTFOUND")
      message(WARNING "Moxa Library not present !")
  else()
      add_definitions(-DUSE_MOXA)
  endif()
  
  find_package(catkin REQUIRED COMPONENTS
    roscpp
    std_msgs
  )
  
  ## System dependencies are found with CMake's conventions
   find_package(Boost REQUIRED COMPONENTS thread)
  
  ###################################
  ## catkin specific configuration ##
  ###################################
  
  catkin_package(
     CATKIN_DEPENDS roscpp std_msgs
  )
  
  ###########
  ## Build ##
  ###########
  
  ## Specify additional locations of header files
  ## Your package locations should be listed before other locations
  # include_directories(include)
  include_directories(
    ${catkin_INCLUDE_DIRS}
    /usr/lib/x86_64-linux-gnu/
  )
  
  ## Declare a cpp executable
   add_executable(main
       src/main.cpp)
  
  ## Specify libraries to link a library or executable target against
   target_link_libraries(main
     ${MOXA_LIBRARY}
     ${catkin_LIBRARIES}
     ${CMAKE_THREAD_LIBS_INIT}
   )
  </code></pre>
  
  <p>My package.xml:</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;package&gt;
    &lt;name&gt;master&lt;/name&gt;
    &lt;version&gt;0.0.0&lt;/version&gt;
    &lt;description&gt;The master package&lt;/description&gt;
  
    &lt;maintainer email="johau@todo.todo"&gt;johau&lt;/maintainer&gt;
  
    &lt;license&gt;BSD&lt;/license&gt;
  
    &lt;buildtool_depend&gt;catkin&lt;/buildtool_depend&gt;
    &lt;build_depend&gt;roscpp&lt;/build_depend&gt;
    &lt;build_depend&gt;std_msgs&lt;/build_depend&gt;
    &lt;run_depend&gt;roscpp&lt;/run_depend&gt;
    &lt;run_depend&gt;std_msgs&lt;/run_depend&gt;
  
  
    &lt;!-- The export tag contains other, unspecified, tags --&gt;
    &lt;export&gt;
      &lt;!-- You can specify that this package is a metapackage here: --&gt;
      &lt;!-- &lt;metapackage/&gt; --&gt;
  
      &lt;!-- Other tools can request additional information be placed here --&gt;
  
    &lt;/export&gt;
  &lt;/package&gt;
  </code></pre>
  
  <p>[EDIT 2]
  After request from @Lu-Niu did I run <code>catkin_make VERBOSE=1</code> (catkin_make should be the same as the make command, as far as I know, the catkin part is just because it's within ROS) and the output is in the box bellow. As far, as I can see, so is -pthread specified, while I think it should be -lpthread, when the directory isn't specified first (correct me, if I'm wrong). So what should I edit, to change the order of the command?</p>
  
  <pre><code>Base path: /home/johau/ros_ws
  Source space: /home/johau/ros_ws/src
  Build space: /home/johau/ros_ws/build
  Devel space: /home/johau/ros_ws/devel
  Install space: /home/johau/ros_ws/install
  ####
  #### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
  ####
  ####
  #### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
  ####
  /usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
  /usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
  make -f CMakeFiles/Makefile2 all
  make[1]: Entering directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
  make[2]: Entering directory `/home/johau/ros_ws/build'
  cd /home/johau/ros_ws/build &amp;&amp; /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
  make[2]: Entering directory `/home/johau/ros_ws/build'
  Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
  cd /home/johau/ros_ws/build/master &amp;&amp; /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
  /usr/bin/c++   -lpthread    CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
  CMakeFiles/main.dir/src/main.cpp.o: In function `main':
  main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
  /usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
  collect2: error: ld returned 1 exit status
  make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
  make[1]: Leaving directory `/home/johau/ros_ws/build'
  make: *** [all] Error 2
  Invoking "make" failed
  </code></pre>
  
  <p>[EDIT 3]
  Both of @fenix688 's suggestions are giving this output (with VERBOSE=1):</p>
  
  <pre><code>Base path: /home/johau/ros_ws
  Source space: /home/johau/ros_ws/src
  Build space: /home/johau/ros_ws/build
  Devel space: /home/johau/ros_ws/devel
  Install space: /home/johau/ros_ws/install
  ####
  #### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
  ####
  #### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
  ####
  /usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
  /usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
  make -f CMakeFiles/Makefile2 all
  make[1]: Entering directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
  make[2]: Entering directory `/home/johau/ros_ws/build'
  cd /home/johau/ros_ws/build &amp;&amp; /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
  make[2]: Entering directory `/home/johau/ros_ws/build'
  Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
  cd /home/johau/ros_ws/build/master &amp;&amp; /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
  /usr/bin/c++       CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lpthread -lboost_thread -lpthread -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
  CMakeFiles/main.dir/src/main.cpp.o: In function `main':
  main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
  /usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
  collect2: error: ld returned 1 exit status
  make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
  make[1]: Leaving directory `/home/johau/ros_ws/build'
  make: *** [all] Error 2
  Invoking "make" failed
  </code></pre>
  
  <p>While if I remove everything with pthread from the CMakeLists.txt do I get this VERBOSE=1 output, where -lpthread still is set.</p>
  
  <pre><code>Base path: /home/johau/ros_ws
  Source space: /home/johau/ros_ws/src
  Build space: /home/johau/ros_ws/build
  Devel space: /home/johau/ros_ws/devel
  Install space: /home/johau/ros_ws/install
  ####
  #### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
  ####
  ####
  #### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
  ####
  /usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
  /usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
  make -f CMakeFiles/Makefile2 all
  make[1]: Entering directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
  make[2]: Entering directory `/home/johau/ros_ws/build'
  cd /home/johau/ros_ws/build &amp;&amp; /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
  make[2]: Entering directory `/home/johau/ros_ws/build'
  Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
  cd /home/johau/ros_ws/build/master &amp;&amp; /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
  /usr/bin/c++       CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
  CMakeFiles/main.dir/src/main.cpp.o: In function `main':
  main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
  /usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
  collect2: error: ld returned 1 exit status
  make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
  make[1]: Leaving directory `/home/johau/ros_ws/build'
  make: *** [all] Error 2
  Invoking "make" failed
  </code></pre>
  
  <p>[EDIT 4]
  Tried with the CMakeLists.txt @fenix688 wrote and it gave the same error with this output:</p>
  
  <pre><code>Base path: /home/johau/ros_ws
  Source space: /home/johau/ros_ws/src
  Build space: /home/johau/ros_ws/build
  Devel space: /home/johau/ros_ws/devel
  Install space: /home/johau/ros_ws/install
  ####
  #### Running command: "make cmake_check_build_system" in "/home/johau/ros_ws/build"
  ####
  ####
  #### Running command: "make VERBOSE=1 -j8 -l8" in "/home/johau/ros_ws/build"
  ####
  /usr/bin/cmake -H/home/johau/ros_ws/src -B/home/johau/ros_ws/build --check-build-system CMakeFiles/Makefile.cmake 0
  /usr/bin/cmake -E cmake_progress_start /home/johau/ros_ws/build/CMakeFiles /home/johau/ros_ws/build/CMakeFiles/progress.marks
  make -f CMakeFiles/Makefile2 all
  make[1]: Entering directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/depend
  make[2]: Entering directory `/home/johau/ros_ws/build'
  cd /home/johau/ros_ws/build &amp;&amp; /usr/bin/cmake -E cmake_depends "Unix Makefiles" /home/johau/ros_ws/src /home/johau/ros_ws/src/master /home/johau/ros_ws/build /home/johau/ros_ws/build/master /home/johau/ros_ws/build/master/CMakeFiles/main.dir/DependInfo.cmake --color=
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make -f master/CMakeFiles/main.dir/build.make master/CMakeFiles/main.dir/build
  make[2]: Entering directory `/home/johau/ros_ws/build'
  Linking CXX executable /home/johau/ros_ws/devel/lib/master/main
  cd /home/johau/ros_ws/build/master &amp;&amp; /usr/bin/cmake -E cmake_link_script CMakeFiles/main.dir/link.txt --verbose=1
  /usr/bin/c++       CMakeFiles/main.dir/src/main.cpp.o  -o /home/johau/ros_ws/devel/lib/master/main  -L/usr/local/lib -rdynamic -lpthread -lmxio_x64 /opt/ros/indigo/lib/libroscpp.so -lboost_signals -lboost_filesystem /opt/ros/indigo/lib/librosconsole.so /opt/ros/indigo/lib/librosconsole_log4cxx.so /opt/ros/indigo/lib/librosconsole_backend_interface.so -llog4cxx -lboost_regex /opt/ros/indigo/lib/libxmlrpcpp.so /opt/ros/indigo/lib/libroscpp_serialization.so /opt/ros/indigo/lib/librostime.so -lboost_date_time /opt/ros/indigo/lib/libcpp_common.so -lboost_system -lboost_thread -lpthread -lconsole_bridge -lboost_thread -lpthread -lconsole_bridge -Wl,-rpath,/usr/local/lib:/opt/ros/indigo/lib 
  CMakeFiles/main.dir/src/main.cpp.o: In function `main':
  main.cpp:(.text+0xdd0): warning: the `gets' function is dangerous and should not be used.
  /usr/local/lib/libmxio_x64.so: undefined reference to `pthread_create'
  collect2: error: ld returned 1 exit status
  make[2]: *** [/home/johau/ros_ws/devel/lib/master/main] Error 1
  make[2]: Leaving directory `/home/johau/ros_ws/build'
  make[1]: *** [master/CMakeFiles/main.dir/all] Error 2
  make[1]: Leaving directory `/home/johau/ros_ws/build'
  make: *** [all] Error 2
  Invoking "make" failed
  </code></pre>