使用CMake进行升压测试-未定义主要 [英] Boost Test with CMake - undefined main

查看:124
本文介绍了使用CMake进行升压测试-未定义主要的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用MacPorts在/opt/local/lib/

I'm having trouble building a little program that uses Boost.Test on my Mac with a Boost installed by MacPorts at /opt/local/lib/

这是我的最小源文件test.cpp:

#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE(test1) {
}

和我的CMakeLists.txt:

cmake_minimum_required(VERSION 2.6)
project (test)
find_package(Boost COMPONENTS unit_test_framework REQUIRED)
add_executable(test test.cpp)

make VERBOSE=1的摘录:

[100%] Building CXX object CMakeFiles/test.dir/test.cpp.o
g++ -o CMakeFiles/test.dir/test.cpp.o -c /Users/exclipy/Code/cpp/inline_variant/question/test.cpp
Linking CXX executable test
"/Applications/CMake 2.8-5.app/Contents/bin/cmake" -E cmake_link_script CMakeFiles/test.dir/link.txt --verbose=1
g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cpp.o-o test
Undefined symbols for architecture x86_64:
  "_main", referenced from:
      start in crt1.10.6.o
  "vtable for boost::unit_test::unit_test_log_t", referenced from:
      boost::unit_test::unit_test_log_t::unit_test_log_t() in test.cpp.o
      boost::unit_test::unit_test_log_t::~unit_test_log_t() in test.cpp.o
  NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.

如您所见,它不知道如何链接到Boost库.所以我尝试添加到CMakeLists.txt:

As you can see, it doesn't know how to link to Boost library. So I try adding to CMakeLists.txt:

target_link_libraries(test boost_unit_test_framework)

但是我得到:

g++ -Wl,-search_paths_first -Wl,-headerpad_max_install_names CMakeFiles/test.dir/test.cpp.o-o test -lboost_unit_test_framework 
ld: library not found for -lboost_unit_test_framework

通过大量的试验和错误,我发现手动运行此功能是可行的:

From lots of trial and error, I've found that manually running this works:

$ g++ test.cpp -L/opt/local/lib -lboost_unit_test_framework -DBOOST_TEST_DYN_LINK

但是经过数小时的摆弄,我无法从CMake构建它.我不在乎它是动态链接还是静态链接,我只是希望它能工作.

But after hours of fiddling, I can't get it to build from CMake. I don't care whether it links dynamically or statically, I just want it to work.

推荐答案

您需要告诉CMake在哪里可以找到Boost库(在g ++行中的-L/opt/local/lib).您可以通过添加以下行来完成此操作(如果您对find_package没问题):

You need to tell CMake where to find the boost libraries (the -L/opt/local/lib in your g++ line). You can accomplish this by adding the following line (if you had no problem with find_package):

link_directories ( ${Boost_LIBRARY_DIRS} )

add_executable之前.

另一种替代方法是使用

Another alternative is using the single-header variant of the UTF. This variant is really simple (you only need to include <boost/test/included/unit_test.hpp> but it has a major drawback in its considerable increase in build time.

这篇关于使用CMake进行升压测试-未定义主要的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆