Installing GTest and GMock - c++

I have installed GTest and GMock on my Ubuntu 17.04 box. Broadly speaking installed as follows;
sudo apt-get install libgtest-dev
cmake CMakeLists.txt
make
I then sym linked the libraries creaetd in /usr/src in /usr/lib. (ie link in /usr/lib points to /usr/src)
I then did the same for GMock
sudo apt-get install -y google-mock
then cmake and make as above
Libraries sym linked as before.
I have created the following CMakeLists.txt
project(chapterFirstExample)
cmake_minimum_required(VERSION 2.6)
include_directories(/usr/include)
link_directories(/usr/lib)
add_definitions(-std=c++0x)
set(CMAKE_CXX_FLAGS "${CMAXE_CXX_FLAGS} -Wall")
set(sources
main.cpp
SoundexTest.cpp)
add_executable(tddTest ${sources})
target_link_libraries(tddTest pthread)
target_link_libraries(tddTest gmock)
target_link_libraries(tddTest gtest)
And have two source files
main.cpp
#include "gmock/gmock.h"
int main(int argc, char** argv) {
testing::InitGoogleMock(&argc, argv);
return RUN_ALL_TESTS();
}
SoundexTest.cpp
class Soundex
{
};
#include "gmock/gmock.h"
TEST(SoundexEncoding, RetainsSoleLetterOfOneLetterWord)
{
Soundex soundex;
}
(I am working through Jeff Langr's Modern C++ Programming with Test Driven Development book)
I run cmake on my CmakeLists.txt file, which runs without error. When I run make though I get this;
[ 33%] Linking CXX executable tddTest
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x24): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEED5Ev]+0x39): undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::~ThreadLocal()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED2Ev[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED5Ev]+0x24): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED2Ev[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEED5Ev]+0x39): undefined reference to `pthread_key_delete'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::GetOrCreateValue() const':
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv]+0x25): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE16GetOrCreateValueEv]+0x88): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::CreateKey()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE9CreateKeyEv]+0x27): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<std::vector<testing::internal::TraceInfo, std::allocator<testing::internal::TraceInfo> > >::CreateKey()':
gtest-all.cc:(.text._ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalISt6vectorINS0_9TraceInfoESaIS3_EEE9CreateKeyEv]+0x27): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gtest-all.cc.o): In function `testing::internal::ThreadLocal<testing::TestPartResultReporterInterface*>::GetOrCreateValue() const':
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv]+0x25): undefined reference to `pthread_getspecific'
gtest-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_31TestPartResultReporterInterfaceEE16GetOrCreateValueEv]+0x88): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gmock-all.cc.o): In function `testing::internal::ThreadLocal<testing::Sequence*>::CreateKey()':
gmock-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_8SequenceEE9CreateKeyEv[_ZN7testing8internal11ThreadLocalIPNS_8SequenceEE9CreateKeyEv]+0x27): undefined reference to `pthread_key_create'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gmock-all.cc.o): In function `testing::internal::ThreadLocal<testing::Sequence*>::GetOrCreateValue() const':
gmock-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv]+0x25): undefined reference to `pthread_getspecific'
gmock-all.cc:(.text._ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv[_ZNK7testing8internal11ThreadLocalIPNS_8SequenceEE16GetOrCreateValueEv]+0x88): undefined reference to `pthread_setspecific'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../../lib/libgmock.a(gmock-all.cc.o): In function `testing::internal::ThreadLocal<testing::Sequence*>::~ThreadLocal()':
gmock-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_8SequenceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_8SequenceEED5Ev]+0x24): undefined reference to `pthread_getspecific'
gmock-all.cc:(.text._ZN7testing8internal11ThreadLocalIPNS_8SequenceEED2Ev[_ZN7testing8internal11ThreadLocalIPNS_8SequenceEED5Ev]+0x39): undefined reference to `pthread_key_delete'
collect2: error: ld returned 1 exit status
CMakeFiles/tddTest.dir/build.make:120: recipe for target 'tddTest' failed
make[2]: *** [tddTest] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/tddTest.dir/all' failed
make[1]: *** [CMakeFiles/tddTest.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
I am new to cmake, and very rusty on make. This output though makes me feel thought that I have not built GTest or GMock correctly. Can anyone give some pointers please. (No pun intended)
Thanks

The order of libraries passed to the linker matters. When the linker sees a library file name, by default (*) it pulls in just the objects from that library which resolve one or more undefined symbols it knows are needed at that point.
So if a library A depends on a library B but you pass main.o -lB -lA to the linker, when it comes to -lB it will pull in some objects from B if they are needed by main.o, but then if A needs additional symbols from B, it will not go back and look for additional objects from B, so those needed symbols remain unresolved.
gmock depends on gtest, and gmock (and maybe gtest?) depend on pthread, so a better order for your libraries would be:
target_link_libraries(tddTest gtest)
target_link_libraries(tddTest gmock)
target_link_libraries(tddTest pthread)
(*) Some GNU ld linker options cause different behavior. In particular, --whole-archive -lsomething --no-whole-archive will force it to get ALL the contents from a library file, needed or not. --start-group -lA -lB -lC --end-group will cause it to repeatedly loop over a set of libraries until none of them can be used to resolve any more symbols for a whole loop. But most of the time, it's enough to just list the libraries you need so that the libraries that use other libraries come earlier in the list.

Related

How to use cmake to compile a project written in C and C++ with dpdk

I am using dpdk-stable-19.11.3, and the NIC is Mellaonx Connect5-X. The project's main function is written in C++, but all other functions it calls are written in C. I use cmake to manage my project. I packed all C code to a static library. Without dpdk, it's fine. After adding dpdk, the CMakeLists.txt seems missing some libs. It compiled successfully. But when running the executable file, it returns "open file error". Then I added libs rte_pmd_mlx5 and ibverbs. The errors occur when running command make.
➜ build git:(master) ✗ make
-- Configuring done
-- Generating done
-- Build files have been written to: /home/hewen/demo/build
[ 83%] Built target mylib
[ 91%] Linking CXX executable demo
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dv_open_device':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:830: undefined reference to `mlx5dv_open_device'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_port_query':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:1030: undefined reference to `mlx5dv_query_devx_port'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_qp_query':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:1012: undefined reference to `mlx5dv_devx_qp_query'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_umem_dereg':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:999: undefined reference to `mlx5dv_devx_umem_dereg'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_umem_reg':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:984: undefined reference to `mlx5dv_devx_umem_reg'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_get_async_cmd_comp':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:969: undefined reference to `mlx5dv_devx_get_async_cmd_comp'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_obj_query_async':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:950: undefined reference to `mlx5dv_devx_obj_query_async'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_destroy_cmd_comp':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:937: undefined reference to `mlx5dv_devx_destroy_cmd_comp'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_create_cmd_comp':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:925: undefined reference to `mlx5dv_devx_create_cmd_comp'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_general_cmd':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:910: undefined reference to `mlx5dv_devx_general_cmd'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_obj_modify':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:893: undefined reference to `mlx5dv_devx_obj_modify'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_obj_query':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:876: undefined reference to `mlx5dv_devx_obj_query'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_obj_destroy':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:863: undefined reference to `mlx5dv_devx_obj_destroy'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_devx_obj_create':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:847: undefined reference to `mlx5dv_devx_obj_create'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dv_destroy_flow_matcher':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:815: undefined reference to `mlx5dv_dr_matcher_destroy'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dv_destroy_flow':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:804: undefined reference to `mlx5dv_dr_rule_destroy'
...
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:517: undefined reference to `mlx5dv_create_wq'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dv_create_cq':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:502: undefined reference to `mlx5dv_create_cq'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_destroy_domain':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:489: undefined reference to `mlx5dv_dr_domain_destroy'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_domain':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:476: undefined reference to `mlx5dv_dr_domain_create'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_destroy_flow_tbl':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:463: undefined reference to `mlx5dv_dr_table_destroy'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_flow_tbl':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:450: undefined reference to `mlx5dv_dr_table_create'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_flow_action_pop_vlan':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:439: undefined reference to `mlx5dv_dr_action_create_pop_vlan'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_flow_action_push_vlan':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:426: undefined reference to `mlx5dv_dr_action_create_push_vlan'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_flow_action_drop':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:414: undefined reference to `mlx5dv_dr_action_create_drop'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_flow_action_dest_port':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:397: undefined reference to `mlx5dv_dr_action_create_dest_ib_port'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_dr_create_flow_action_dest_flow_tbl':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:385: undefined reference to `mlx5dv_dr_action_create_dest_table'
/home/hewen/dpdk-stable-19.11.3/x86_64-native-linuxapp-gcc/lib/librte_pmd_mlx5.a(mlx5_glue.o): In function `mlx5_glue_destroy_flow_action':
/home/hewen/dpdk-stable-19.11.3/drivers/net/mlx5/mlx5_glue.c:190: undefined reference to `mlx5dv_dr_action_destroy'
collect2: error: ld returned 1 exit status
CMakeFiles/demo.dir/build.make:95: recipe for target 'demo' failed
make[2]: *** [demo] Error 1
CMakeFiles/Makefile2:104: recipe for target 'CMakeFiles/demo.dir/all' failed
Is there any libs I missed? This is my CMakeLists.txt.
cmake_minimum_required(VERSION 3.10)
project(myPJ LANGUAGES C CXX)
find_package(Threads REQUIRED)
set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_FLAGS "-msse4.2")
option(DEBUG "add complie options for gdb" ON)
set(RTE_SDK /home/hewen/dpdk-stable-19.11.3)
set(RTE_TARGET x86_64-native-linuxapp-gcc)
include_directories(${RTE_SDK}/${RTE_TARGET}/include)
link_directories(${RTE_SDK}/${RTE_TARGET}/lib)
include_directories(./tools)
link_directories("./tools/")
add_definitions(-pthread)
if(DEBUG)
add_compile_options(-g -O0)
endif()
add_definitions(-DBUCKET_CLEANER)
add_definitions(-DCOLLECT_STATS -DTABLE_COLLECT_STATS -DLOG_COLLECT_STATS)
add_definitions(-DVERBOSE)
add_library(mylib STATIC a.c b.c c.c d.c)
add_executable(demo main.cpp)
target_link_libraries(demo PRIVATE mylib)
target_link_libraries(demo PRIVATE libxxhash.so)
target_link_libraries(demo PRIVATE Threads::Threads)
target_link_libraries(demo PRIVATE
-Wl,--whole-archive
rte_ethdev
rte_eal
rte_mempool
rte_pmd_mlx5
ibverbs
rte_pmd_e1000
rte_pmd_i40e
rte_pmd_ixgbe
rte_pmd_virtio
rte_pmd_vmxnet3_uio
rte_hash
rte_kvargs
rte_bus_pci
rte_pci
rte_bus_vdev
rte_mbuf
rte_ring
rte_net
pthread
dl
-Wl,--no-whole-archive
numa
rt
pcap
m
)
If using custom Makefile or CMakeLists.txt, one has to ensure the following
DPDK is built with appropriate MXL5 PMD
For DPDK Makefile is used, libraries added to build path are
EXTRA_CFLAGS=-I/path/to/rdma-core/build/include
EXTRA_LDFLAGS=-L/path/to/rdma-core/build/lib
PKG_CONFIG_PATH=/path/to/rdma-core/build/lib/pkgconfig
Cross check and verify example code like l2fwd and skeleton works.
note: as mentioned in the comment it is useful to check
https://doc.dpdk.org/guides/nics/mlx5.html
check the build flags for example/l2fwde in build folder too.
[EDIT-1] as per the update in comment, the problem is resolved by including mxl5 library to CMakeLists.txt.

How to build a static boost library?

More specifically static boost::iostreams with zlib (aka libz) support.
The problem is, that when I try to link static library it throws unresolved symbols. At the same time when I try to link dynamic library everything works...
I've been searching for answer for several days and can't find it yet.
The command I use to build shared library: b2 -a -q -j8 address-model=32 toolset=gcc --with-iostreams link=shared runtime-link=shared -sZLIB_INCLUDE="path" -sZLIB_LIBPATH="path" -sBZIP2_INCLUDE="path" -sBZIP2_LIBPATH="path"
The command I use to build static library: b2 -a -q -j8 address-model=32 toolset=gcc --with-iostreams link=static runtime-link=static -sZLIB_INCLUDE="path" -sZLIB_LIBPATH="path" -sBZIP2_INCLUDE="path" -sBZIP2_LIBPATH="path"
Example of the program:
#include <iostream>
#include <boost/iostreams/filter/gzip.hpp>
int main(int argc, char* argv[]) {
int a = boost::iostreams::zlib::default_compression;
std::cout << a << std::endl;
return 0;
}
The exception I get:
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x124): undefined reference to `crc32'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x181): undefined reference to `deflate'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1a1): undefined reference to `inflate'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1cc): undefined reference to `deflateReset'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1e5): undefined reference to `inflateEnd'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x201): undefined reference to `inflateReset'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x214): undefined reference to `deflateEnd'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x3c9): undefined reference to `inflateInit2_'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x469): undefined reference to `deflateInit2_'
collect2.exe: error: ld returned 1 exit status
makefile:15: recipe for target 'Source.exe' failed
mingw32-make: *** [Source.exe] Error 1
The terminal process terminated with exit code: 1
UPDATE:
Also in case the problem may be related to zlib or bz2:
I got bz2 lib here: http://gnuwin32.sourceforge.net/packages/bzip2.htm
and zlib here: http://gnuwin32.sourceforge.net/packages/zlib.htm
And I tried to build zlib myself with their win32 makefile. I got libz.a and tried to build with it as well, nothing's changed.
As you may know, to solve this problem on unix you just need to add -lz argument.
The same works for windows users. You just (more likely) don't have zlib.a/lib location in your PATH environment variable. So to solve the problem you shall add 2 arguments: -Lpath_to_zlib -lz. Such a simple solution and I've spent hours trying to fix it.

Boost "undefined reference" errors even with -lboost_thread

I'm getting some strange compiler/linker errors when trying to use boost::shared_mutex. I'm using boost v1.61 on a VM running 32-bit rhel 6.2.
Code that causes error:
hpp file:
#include <boost/thread/shared_mutex.hpp>
class SharedData
{
public:
SharedData();
~SharedData();
void packMessage(std::shared_ptr<Message> s);
private:
// mutex that allows multiple read, single write protection
boost::shared_mutex m_sharedMutex;
};
cpp file:
void SharedData::packMessage(std::shared_ptr<Message> s)
{
// get shared read access
boost::shared_lock<boost::shared_mutex> lock(m_sharedMutex); // <- this line causes the errors
// read stuff here
}
make output:
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:195: undefined reference to `boost::detail::get_current_thread_data()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:81: undefined reference to `boost::this_thread::interruption_point()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::shared_mutex::lock_shared()':
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
collect2: error: ld returned 1 exit status
make[2]: *** [bin/tester] Error 1
Everywhere I've searched has said that these errors mean I need to link the boost_thread library, which I've done in my cmake file (boost_system is included for other code in this same project):
target_link_libraries(${BINARY_NAME} boost_thread boost_system)
But the errors persist.
Originally I was using the boost 1.41 libraries that were already installed on my machine. When I hit these errors I ran yum remove boost-devel and then manually installed boost 1.61 to see if that would correct the errors. Is there something extra I needed to add to the ./bootstrap.sh or ./b2 install commands? I can see libboost_thread.a in /usr/local/lib, so I assumed that meant the thread library was built correctly.
Any thoughts on what is causing these errors? Thanks!
Edit:
Cmake's log didn't have anything useful I could see, but make VERBOSE=1 gives the following output:
cd /home/craig/dev/myProject/build/test && /usr/bin/cmake -E cmake_link_script CMakeFiles/tester.dir/link.txt --verbose=1
/opt/rh/devtoolset-2/root/usr/bin/c++ -std=c++11 -ggdb -Wall -Werror -fprofile-arcs -ftest-coverage -fPIC -O0 -pedantic -Wl,--export-dynamic CMakeFiles/tester.dir/utilities/googletest/googletest/src/gtest-all.cc.o CMakeFiles/tester.dir/tester.cpp.o CMakeFiles/tester.dir/SharedData/testSharedData.cpp.o CMakeFiles/tester.dir/Common/testFifo.cpp.o CMakeFiles/tester.dir/Common/testCsu.cpp.o CMakeFiles/tester.dir/Messages/testMessage.cpp.o CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o CMakeFiles/tester.dir/__/src/Common/Fifo.cpp.o CMakeFiles/tester.dir/__/src/Common/Csu.cpp.o -o ../bin/tester -rdynamic -lboost_system -lpthread
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::detail::interruption_checker::interruption_checker(pthread_mutex_t*, pthread_cond_t*)':
/usr/local/include/boost/thread/pthread/thread_data.hpp:195: undefined reference to `boost::detail::get_current_thread_data()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::condition_variable::wait(boost::unique_lock<boost::mutex>&)':
/usr/local/include/boost/thread/pthread/condition_variable.hpp:81: undefined reference to `boost::this_thread::interruption_point()'
CMakeFiles/tester.dir/__/src/SharedData/SharedData.cpp.o: In function `boost::shared_mutex::lock_shared()':
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:186: undefined reference to `boost::this_thread::disable_interruption::disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
/usr/local/include/boost/thread/pthread/shared_mutex.hpp:193: undefined reference to `boost::this_thread::disable_interruption::~disable_interruption()'
Turns out my question is just like everyone else's.
#jww suggested I show the actual compile and link command invocation, not CMake's output, which I've added to my question. As you can see, the command is linking boost_system and pthread, but not boost_thread.
Digging into my project I realized that I had added boost_thread to my release build, but not into my unit test build, which is what I was trying to compile. Adding boost_thread to the unit test's CMakeLists.txt removed the error immediately.

Compiling Box2D HelloWorld on Ubuntu 12.04

I am planning to use Box2D in my C++ project. I have downloaded the latest version (v2.3.0.7 when asking this question) of Box2D and built it successfully. I can run the Testbed without any errors.
Now, I am trying to build the HelloWorld.cpp example, which resides in HelloWorld folder in Box2D source files but I can't compile this example.
Below is my command for compiling:
g++ -g -Wall -L/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D/Build/Box2D -lBox2D -I/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D HelloWorld.cpp -o Hello
And this is the output I get:
/tmp/cc2U314E.o: In function `main':
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:37: undefined reference to `b2World::b2World(b2Vec2 const&)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:46: undefined reference to `b2World::CreateBody(b2BodyDef const*)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:52: undefined reference to `b2PolygonShape::SetAsBox(float, float)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:55: undefined reference to `b2Body::CreateFixture(b2Shape const*, float)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:61: undefined reference to `b2World::CreateBody(b2BodyDef const*)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:65: undefined reference to `b2PolygonShape::SetAsBox(float, float)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:78: undefined reference to `b2Body::CreateFixture(b2FixtureDef const*)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:92: undefined reference to `b2World::Step(float, int, int)'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:104: undefined reference to `b2World::~b2World()'
/home/viki/Desktop/collision_test/test/HelloWorld.cpp:104: undefined reference to `b2World::~b2World()'
/tmp/cc2U314E.o: In function `b2PolygonShape':
/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D/Box2D/Collision/Shapes/b2PolygonShape.h:87: undefined reference to `vtable for b2PolygonShape'
/tmp/cc2U314E.o: In function `~b2PolygonShape':
/home/viki/Desktop/collision_test/Box2D_v2.3.0/Box2D/Box2D/Collision/Shapes/b2PolygonShape.h:28: undefined reference to `vtable for b2PolygonShape'
collect2: ld returned 1 exit status
I'm using
-L flag for searching for libBox2D.a
-l flag for linking Box2D
-I flag for pointing to the Box2D headers directory
What am I missing? Is there any specific library or command argument I am supposed to add?
Put the -lBox2D behind the HelloWorld.cpp
If you have built your project properly the HelloWorld example will be automatically built for you. There's a CMakeLists.txt file in the example folder just run cmake CMakeLists.txt in the folder and then run make. Prior to this you must have built and installed Box2D library. Here is what I did:
I entered the Build directory within the Box2D and typed the following command:
cmake -DBOX2D_INSTALL=ON -DBOX2D_BUILD_SHARED=ON -DCMAKE_INSTALL_PREFIX=/opt/Box2D ..
Then I went one level up to Box2D directory and ran make then sudo make install.
Then I entered the HelloWorld directory and it was automatically built. I didn't have to build it manually. Also this is the output from make that showed the example was built:
Linking CXX static library libBox2D.a
[ 42%] Built target Box2D
[ 43%] Building CXX object HelloWorld/CMakeFiles/HelloWorld.dir/HelloWorld.cpp.o

FLTK 1.3 Linking errors

I update my FLTK from 1.1 to 1.3, then I can't compile my code which is worked well before update.
...
[100%] Building CXX object CMakeFiles/PROSTVIEW.dir/include/nifti/vtkznzlib.cxx.o
Linking CXX executable PROSTVIEW
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fontopen(char const*, int, bool, int)':
fl_font.cxx:(.text+0x298): undefined reference to `FcPatternCreate'
fl_font.cxx:(.text+0x362): undefined reference to `FcPatternAddString'
fl_font.cxx:(.text+0x3e3): undefined reference to `FcPatternAddString'
fl_font.cxx:(.text+0x3f7): undefined reference to `FcPatternAddInteger'
fl_font.cxx:(.text+0x40b): undefined reference to `FcPatternAddInteger'
fl_font.cxx:(.text+0x424): undefined reference to `FcPatternAddDouble'
fl_font.cxx:(.text+0x43c): undefined reference to `FcPatternAddString'
fl_font.cxx:(.text+0x4df): undefined reference to `FcMatrixRotate'
fl_font.cxx:(.text+0x4f7): undefined reference to `FcPatternAddMatrix'
fl_font.cxx:(.text+0x516): undefined reference to `FcPatternAddBool'
fl_font.cxx:(.text+0x52c): undefined reference to `FcPatternAddBool'
fl_font.cxx:(.text+0x549): undefined reference to `XftFontMatch'
fl_font.cxx:(.text+0x5a0): undefined reference to `XftFontOpen'
fl_font.cxx:(.text+0x5b0): undefined reference to `FcPatternDestroy'
fl_font.cxx:(.text+0x5f3): undefined reference to `XftFontOpenPattern'
fl_font.cxx:(.text+0x603): undefined reference to `FcPatternDestroy'
fl_font.cxx:(.text+0x659): undefined reference to `XftFontOpenXlfd'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `utf8extents(Fl_Font_Descriptor*, char const*, int, _XGlyphInfo*)':
fl_font.cxx:(.text+0x84b): undefined reference to `XftTextExtents32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fl_xft_width(Fl_Font_Descriptor*, unsigned int*, int)':
fl_font.cxx:(.text+0x98a): undefined reference to `XftTextExtents32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fl_destroy_xft_draw(unsigned long)':
fl_font.cxx:(.text+0x102e): undefined reference to `XftDrawChange'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `Fl_Xlib_Graphics_Driver::draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x10c6): undefined reference to `XftDrawCreate'
fl_font.cxx:(.text+0x10f6): undefined reference to `XftDrawChange'
fl_font.cxx:(.text+0x1140): undefined reference to `XftDrawSetClip'
fl_font.cxx:(.text+0x1202): undefined reference to `XftDrawString32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `fl_drawUCS4(Fl_Graphics_Driver*, unsigned int const*, int, int, int)':
fl_font.cxx:(.text+0x1313): undefined reference to `XftDrawCreate'
fl_font.cxx:(.text+0x1343): undefined reference to `XftDrawChange'
fl_font.cxx:(.text+0x138d): undefined reference to `XftDrawSetClip'
fl_font.cxx:(.text+0x1433): undefined reference to `XftDrawString32'
/usr/local/lib/libfltk.a(fl_font.cxx.o): In function `Fl_Xlib_Graphics_Driver::rtl_draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x1480): undefined reference to `FcUtf8Len'
fl_font.cxx:(.text+0x14ff): undefined reference to `FcUtf8ToUcs4'
collect2: ld returned 1 exit status
make[2]: *** [PROSTVIEW] Error 1
make[1]: *** [CMakeFiles/PROSTVIEW.dir/all] Error 2
make: *** [all] Error 2
Here's my CMakeLists.txt
cmake_minimum_required(VERSION 2.6)
PROJECT(PROSTVIEW)
SET(CMAKE_BUILD_TYPE DEBUG)
SET(CMAKE_CXX__FLAGS -O3 -pipe -ffast-math -Wno-deprecated)
# Chargement Package
# FLTK
FIND_PACKAGE ( FLTK )
IF ( FLTK_FOUND )
INCLUDE_DIRECTORIES( ${FLTK_INCLUDE_DIR} )
ENDIF ( FLTK_FOUND )
# ITK
FIND_PACKAGE ( ITK )
IF ( ITK_FOUND )
INCLUDE( ${USE_ITK_FILE} )
ENDIF( ITK_FOUND )
# VTK
FIND_PACKAGE ( VTK )
IF ( VTK_FOUND )
INCLUDE( ${USE_VTK_FILE} )
ENDIF( VTK_FOUND )
SET(VTK_LIBRARIES vtkCommon vtkRendering vtkWidgets vtkGraphics vtkGenericFiltering vtkzlib)
# vtkFlRenderWindowInteractor
SET(VTKFL_INCLUDE_DIR "include/vtkfl")
SET(VTKFL_CPP
${VTKFL_INCLUDE_DIR}/vtkFlRenderWindowInteractor.cxx)
# Auxiliary_Tools
SET(AT_DIR "include/Auxiliary_Tools")
SET(AT_INCLUDE_DIR ${AT_DIR}/include)
SET(AT_LIBRARY_DIR ${AT_DIR}/lib)
#Nifty
SET(NII_DIR "include/nifti")
SET(NII_SRC
${NII_DIR}/vtkNIfTIReader.cxx
${NII_DIR}/vtkNIfTIWriter.cxx
${NII_DIR}/vtkAnalyzeReader.cxx
${NII_DIR}/vtkAnalyzeWriter.cxx
${NII_DIR}/vtknifti1_io.cxx
${NII_DIR}/vtkznzlib.cxx)
#Other include
SET(PROSTVIEW_INCLUDE_DIR "include")
SET(PROSTVIEW_SRC
${PROSTVIEW_INCLUDE_DIR}/ContourTmt.cpp
${PROSTVIEW_INCLUDE_DIR}/conversion.cpp
${PROSTVIEW_INCLUDE_DIR}/TmtMatrix.cpp)
# VOLUME Widget
SET(VOLUME_WIDGET_DIR "include/GUI")
SET(VOLUME_WIDGET
${VOLUME_WIDGET_DIR}/VOLUME_Loader.cpp
${VOLUME_WIDGET_DIR}/VOLUME_Widget.cpp
${VOLUME_WIDGET_DIR}/VOLUME_Mouse_Widget.cpp
${VOLUME_WIDGET_DIR}/VOLUME_3DView.cpp
${VOLUME_WIDGET_DIR}/CONTOUR_Loader.cpp
${VOLUME_WIDGET_DIR}/ProstView.cpp)
# MODULES
SET(MODULE_DIR "Module")
# SEGMENTATION
SET(SEGMENTATION_DIR "Module/Segmentation")
SET(SEGMENTATION
${SEGMENTATION_DIR}/DDC2D.cpp
${SEGMENTATION_DIR}/DDC3D.cpp
${SEGMENTATION_DIR}/DDC3DHu.cpp
${SEGMENTATION_DIR}/DDC3DPlus.cpp
${SEGMENTATION_DIR}/DDC3DBal.cpp
${SEGMENTATION_DIR}/IMAGE_ENERGY.cpp
${SEGMENTATION_DIR}/REGULARIZATION.cpp
${SEGMENTATION_DIR}/OPTIMAL_SURFACE_DETECTION.cpp
${SEGMENTATION_DIR}/OPTIMAL_MULTIPLE_SURFACES_DETECTION.cpp
${SEGMENTATION_DIR}/RESAMPLE_From_Prostate.cpp
${SEGMENTATION_DIR}/RECTAL_WALL_US.cpp
${SEGMENTATION_DIR}/BLADDER_MRI.cpp
${SEGMENTATION_DIR}/RECTUM_MRI.cpp
${SEGMENTATION_DIR}/REGION_GROWING.tpp )
#MESH
SET(MESH_DIR "Module/Mesh")
SET(MESH
${MESH_DIR}/GENERIC_MESH.cpp
${MESH_DIR}/ELLIPSOID_MESH.cpp
${MESH_DIR}/PROSTATE_MESH.cpp
${MESH_DIR}/TUBULAR_MESH.cpp
${MESH_DIR}/OPEN_MESH.cpp
${MESH_DIR}/QUADRIC_MESH.cpp
${MESH_DIR}/ELLIPTICAL_CYLINDER_MESH.cpp
${MESH_DIR}/CONTOURS_MAN_MESH.cpp)
#STUDY
SET(STUDY_DIR "Module/Statistics")
SET(STUDY
${STUDY_DIR}/PROFILE.cpp
${STUDY_DIR}/Histogram.cpp
${STUDY_DIR}/HISTOGRAM_FROM_MESH.cpp
${STUDY_DIR}/TRAINING_SET_ALIGNMENT.cpp
${STUDY_DIR}/APPEARANCE_MODEL.cpp)
# MOMENTS
SET(MOMENTS_DIR "Module/Moments")
SET(MOMENTS
${MOMENTS_DIR}/KRAWTCHOUK_Moments.tpp
${MOMENTS_DIR}/TCHEBICHEF_Moments.tpp
${MOMENTS_DIR}/GEOMETRIC_Moments.tpp)
#VALIDATION
SET(VALIDATION_DIR "Module/Evaluation")
SET(VALIDATION
${VALIDATION_DIR}/Metrics.cpp)
# Include et Link Directories
INCLUDE_DIRECTORIES(${FLTK_INCLUDE_DIR} ${VTK_INCLUDE_DIRS} ${VTKFL_INCLUDE_DIR} ${AT_INCLUDE_DIR} ${NII_DIR} ${PROSTVIEW_INCLUDE_DIR} ${VOLUME_WIDGET_DIR} ${MODULE_DIR})
LINK_DIRECTORIES(${FLTK_LIBRARY_DIR} ${VTK_LIBRARY_DIRS} ${AT_LIBRARY_DIR} ${PROSTVIEW_INCLUDE_DIR} ${VOLUME_WIDGET_DIR} ${MODULE_DIR})
# Sources
SET(SRC main.cpp)
# Executables and "stand-alone " librairies
ADD_EXECUTABLE(PROSTVIEW ${SRC} ${PROSTVIEW_SRC} ${VTKFL_CPP} ${VOLUME_WIDGET} ${SEGMENTATION} ${MESH} ${STUDY} ${MOMENTS} ${VALIDATION} ${NII_SRC})
# Linkage
TARGET_LINK_LIBRARIES(PROSTVIEW ${FLTK_LIBRARIES} ${VTK_LIBRARIES} ${ITK_LIBRARIES})
I tried to add some libs in my CMakeLists.txt, but it's not work. Who can tell me what’s the problem?
From FLTK 1.3's README.CMake.txt:
USING CMAKE WITH FLTK
This howto assumes that you have FLTK libraries which were built using
CMake, installed. Building them with CMake generates some CMake
helper files which are installed in standard locations, making FLTK
easy to find and use.
Here is a basic CMakeLists.txt file using FLTK.
cmake_minimum_required(VERSION 2.6)
project(hello)
find_package(FLTK REQUIRED NO_MODULE) include(${FLTK_USE_FILE})
add_executable(hello WIN32 hello.cxx)
target_link_libraries(hello fltk)
The find_package command tells CMake to find the package FLTK,
REQUIRED means that it is an error if it's not found. NO_MODULE tells
it to search only for the FLTKConfig file, not using the
FindFLTK.cmake supplied with CMake, which doesn't work with this
version of FLTK.
Once the package is found we include the ${FLTK_USE_FILE} which adds
the FLTK include directories and library link information to its
knowledge base. After that your programs will be able to find FLTK
headers and when you link the fltk library, it automatically links the
libraries fltk depends on.
It seems that from FLTK 1.1 to 1.3 they changed the method needed to find the library, maybe for comparability/testing reasons?
I got hung up on the same issue, hope someone finds this useful. :)