您好我想为一个项目制作更多可执行文件。我尝试了一些方法,但我无法弄清楚出了什么问题。
首先,我想制作2个可执行文件:一个用于项目,另一个用于使用GoogleTest进行测试。
我尝试了类似的东西:
if (CMAKE_USE_PTHREADS_INIT)
add_executable(${EXECUTABLE_NAME} ${common_source_files_cpp})
add_executable(${EXECUTABLE_NAME}_test ${common_source_test_files_cpp})
ENABLE_TESTING()
ADD_TEST(${EXECUTABLE_NAME}_test ${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME}_test)
target_link_libraries(${EXECUTABLE_NAME} "-static -lmysqlclient -lz -pthread -ldl -lboost_system-mt -lboost_thread-mt -lboost_program_options-mt -lboost_regex-mt")
target_link_libraries(${EXECUTABLE_NAME}_test "-lmysqlclient -lz -pthread -ldl -lboost_system-mt -lboost_thread-mt -lboost_program_options-mt -lboost_regex-mt")
endif()
但我得到错误。所以我在互联网上搜索了另一种方式并找到了this example
我试过这样做:
cmake_minimum_required(VERSION 2.8)
option(test "Build all tests." OFF)
set(EXECUTABLE_NAME MyProj)
project(${EXECUTABLE_NAME})
set(CMAKE_CXX_FLAGS "-g -Wall")
include_directories(src/main/cpp
${Boost_INCLUDE_DIRS}
)
find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS filesystem
system
regex
program_options
)
add_executable(${EXECUTABLE_NAME}_Sol
src/main/cpp/main.cpp
src/main/cpp/MyTypes.hpp
src/main/cpp/MyFunc.cpp
src/main/cpp/MyFunc.hpp
)
target_link_libraries(${EXECUTABLE_NAME}_Sol2 ${OpenCV_LIBS}
${Boost_LIBRARIES}
)
if (test)
find_package(GTest REQUIRED)
enable_testing()
include_directories( ${GTEST_INCLUDE_DIRS}
)
add_executable(${EXECUTABLE_NAME}_Sol2_test
src/test/cpp/test_Sol2.cpp
src/main/cpp/MyFunc.hpp
src/main/cpp/MyFunc.cpp
)
target_link_libraries(${EXECUTABLE_NAME}_Sol2_test ${OpenCV_LIBS}
${Boost_LIBRARIES}
${GTEST_BOTH_LIBRARIES}
pthread
)
add_test(${EXECUTABLE_NAME}_Sol2_test
${CMAKE_CURRENT_BINARY_DIR}/${EXECUTABLE_NAME}_Sol2_test
)
endif()
现在我遇到了某种链接错误:
/usr/lib/gcc/i686-linux-gnu/4.6/../../../../lib/libgtest.a(gtest-all.cc.o): In function `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)':
gtest-all.cc:(.text+0xdd84): multiple definition of `testing::internal::GTestLog::GTestLog(testing::internal::GTestLogSeverity, char const*, int)'
/usr/local/lib/libopencv_ts.a(ts_gtest.cpp.o):ts_gtest.cpp:(.text._ZN7testing8internal8GTestLogC2ENS0_16GTestLogSeverityEPKci+0x0): first defined here
...
有人能帮助我吗?问题是什么?我做得不好?
好的,它说重新定义是在OpenCV中。但是,如果我删除OpenCV或GTest中的一个,我会得到像
这样的错误/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
/usr/bin/ld: /usr/lib/debug/usr/lib/i386-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
...
有没有办法解决这些重新定义?