我正在使用Google测试进行C ++项目。但是,在对其他.cpp文件进行了一些处理之后,cmake将无法再链接我的项目。 它似乎适用于test.cpp和test.h,但不适用于我添加的新cpp文件(位于Backend / util下),即使它们位于子目录中(我也将它们添加到add_library中)
我已经解决这个问题好几天了,但是无法解决。 我很高兴收到您的评论。
-我的项目目录结构-
-CMake / CompileOptions.cmake
# Install script for directory: /home/jwkim/projects/CubbyDNN
# Set the install prefix
if(NOT DEFINED CMAKE_INSTALL_PREFIX)
set(CMAKE_INSTALL_PREFIX "/usr/local")
endif()
string(REGEX REPLACE "/$" "" CMAKE_INSTALL_PREFIX "${CMAKE_INSTALL_PREFIX}")
# Set the install configuration name.
if(NOT DEFINED CMAKE_INSTALL_CONFIG_NAME)
if(BUILD_TYPE)
string(REGEX REPLACE "^[^A-Za-z0-9_]+" ""
CMAKE_INSTALL_CONFIG_NAME "${BUILD_TYPE}")
else()
set(CMAKE_INSTALL_CONFIG_NAME "Debug")
endif()
message(STATUS "Install configuration: \"${CMAKE_INSTALL_CONFIG_NAME}\"")
endif()
# Set the component getting installed.
if(NOT CMAKE_INSTALL_COMPONENT)
if(COMPONENT)
message(STATUS "Install component: \"${COMPONENT}\"")
set(CMAKE_INSTALL_COMPONENT "${COMPONENT}")
else()
set(CMAKE_INSTALL_COMPONENT)
endif()
endif()
# Install shared libraries without execute permission?
if(NOT DEFINED CMAKE_INSTALL_SO_NO_EXE)
set(CMAKE_INSTALL_SO_NO_EXE "1")
endif()
# Is this installation the result of a crosscompile?
if(NOT DEFINED CMAKE_CROSSCOMPILING)
set(CMAKE_CROSSCOMPILING "FALSE")
endif()
if(NOT CMAKE_INSTALL_LOCAL_ONLY)
# Include the install script for each subdirectory.
include("/home/jwkim/projects/CubbyDNN/cmake-build-debug/Libraries/googlebenchmark/cmake_install.cmake")
include("/home/jwkim/projects/CubbyDNN/cmake-build-debug/Sources/cubbydnn/cmake_install.cmake")
include("/home/jwkim/projects/CubbyDNN/cmake-build-debug/Tests/UnitTests/cmake_install.cmake")
endif()
if(CMAKE_INSTALL_COMPONENT)
set(CMAKE_INSTALL_MANIFEST "install_manifest_${CMAKE_INSTALL_COMPONENT}.txt")
else()
set(CMAKE_INSTALL_MANIFEST "install_manifest.txt")
endif()
string(REPLACE ";" "\n" CMAKE_INSTALL_MANIFEST_CONTENT
"${CMAKE_INSTALL_MANIFEST_FILES}")
file(WRITE "/home/jwkim/projects/CubbyDNN/cmake-build-debug/${CMAKE_INSTALL_MANIFEST}"
"${CMAKE_INSTALL_MANIFEST_CONTENT}")
Tests / UnitTests.cmakeLists.txt
# Target name
set(target UnitTests)
# Includes
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
# Sources
file(GLOB sources
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp)
# Build executable
add_executable(${target}
${sources})
# Project options{
set_target_properties(${target}
PROPERTIES
${DEFAULT_PROJECT_OPTIONS}
)
# Compile options
# GCC and Clang compiler options
if(CMAKE_CXX_COMPILER_ID MATCHES "GNU" OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
set(DEFAULT_COMPILE_OPTIONS ${DEFAULT_COMPILE_OPTIONS}
# /wd4996 # -> disable warning: non-Standard std::tr1 namespace and TR1-only machinery (because of gtest)
-Wno-unused-variable
)
endif()
target_compile_options(${target}
PRIVATE
${DEFAULT_COMPILE_OPTIONS}
)
target_compile_definitions(${target}
PRIVATE
)
# Link libraries
target_link_libraries(${target}
PRIVATE
${DEFAULT_LINKER_OPTIONS}
CubbyDNN
gtest)
-Sources / cubbydnn / cmakeLists.txt
# Target name
set(target CubbyDNN)
# Define
set(root_dir ${CMAKE_CURRENT_SOURCE_DIR}/../..)
# Includes
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}
${CMAKE_CURRENT_SOURCE_DIR}/../../Libraries
)
#Added by Justin
include_directories(
${CMAKE_CURRENT_SOURCE_DIR}/../../Includes
)
# Sources
file(GLOB header_dir
${root_dir}/Includes)
file(GLOB_RECURSE headers
${header_dir}/*.h)
file(GLOB_RECURSE sources
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp Backend/storage/Tensor_object.cpp Backend/storage/baseOp.cpp Backend/storage/storage.cpp)
# Build library
add_library(${target}
${sources} Backend/util/management.cpp Backend/util/baseOp.cpp Backend/util/Tensor_container.cpp)
# Project options
set_target_properties(${target}
PROPERTIES
${DEFAULT_PROJECT_OPTIONS}
)
# Compile options
target_compile_options(${target}
PRIVATE
PUBLIC
${DEFAULT_COMPILE_OPTIONS}
INTERFACE
)
target_link_libraries(${target}
PRIVATE
PUBLIC
${DEFAULT_LINKER_OPTIONS}
${DEFAULT_LIBRARIES}
INTERFACE
)
# Install
install(TARGETS ${target} DESTINATION lib)
install(DIRECTORY ${header_dir} DESTINATION include)
最后,这是我的simpleTest.cpp文件,它执行Google测试
#include <gtest/gtest.h>
#include "../../../../Includes/cubbydnn/Backend/util/management.hpp"
#include "../../../../Includes/cubbydnn/Backend/util/exceptions.hpp"
#include "../../../../Includes/cubbydnn/Backend/util/baseOp.hpp"
#include "../../../../Includes/cubbydnn/Backend/util/Tensor_container.hpp"
#include <Test.h>
using namespace cubby_dnn;
TEST(SimpleTest, Add) {
EXPECT_EQ(5, Add(2, 3));
std::cout<<"called Test"<<std::endl;
emptyOp<int> empty = emptyOp<int>();
empty.print();
}
我在Includes / cubbydnn / Backend / util / baseOp.hpp中声明了emptyOp 并在Sources / cubbydnn / Backend / util / baseOp.cpp
中定义了其方法编译器抱怨(实际上是链接器)
/home/jwkim/clion-2018.2.5/bin/cmake/linux/bin/cmake --build /home/jwkim/projects/CubbyDNN/cmake-build-debug --target gtest -- -j 4
[100%] Built target gtest
Build finished
/home/jwkim/clion-2018.2.5/bin/cmake/linux/bin/cmake --build /home/jwkim/clion-2018.2.5/bin/cmake/linux/bin/cmake --build /home/jwkim/projects/CubbyDNN/cmake-build-debug --target gtest -- -j 4
[100%] Built target gtest
Build finished
/home/jwkim/clion-2018.2.5/bin/cmake/linux/bin/cmake --build /home/jwkim/projects/CubbyDNN/cmake-build-debug --target UnitTests -- -j 4
[ 20%] Built target gtest
[ 70%] Built target CubbyDNN
[ 80%] Linking CXX executable ../../bin/UnitTests
CMakeFiles/UnitTests.dir/SimpleTest.cpp.o: In function `SimpleTest_Add_Test::TestBody()':
/home/jwkim/projects/CubbyDNN/Tests/UnitTests/SimpleTest.cpp:13: undefined reference to `cubby_dnn::emptyOp<int>::emptyOp()'
/home/jwkim/projects/CubbyDNN/Tests/UnitTests/SimpleTest.cpp:14: undefined reference to `cubby_dnn::emptyOp<int>::print()'
collect2: error: ld returned 1 exit status
Tests/UnitTests/CMakeFiles/UnitTests.dir/build.make:100: recipe for target 'bin/UnitTests' failed
make[3]: *** [bin/UnitTests] Error 1
CMakeFiles/Makefile2:360: recipe for target 'Tests/UnitTests/CMakeFiles/UnitTests.dir/all' failed
make[2]: *** [Tests/UnitTests/CMakeFiles/UnitTests.dir/all] Error 2
CMakeFiles/Makefile2:372: recipe for target 'Tests/UnitTests/CMakeFiles/UnitTests.dir/rule' failed
make[1]: *** [Tests/UnitTests/CMakeFiles/UnitTests.dir/rule] Error 2
Makefile:229: recipe for target 'UnitTests' failed
make: *** [UnitTests] Error 2/home/jwkim/projects/CubbyDNN/cmake-build-debug --target UnitTests -- -j 4
[ 20%] Built target gtest
[ 70%] Built target CubbyDNN
[ 80%] Linking CXX executable ../../bin/UnitTests
CMakeFiles/UnitTests.dir/SimpleTest.cpp.o: In function `SimpleTest_Add_Test::TestBody()':
/home/jwkim/projects/CubbyDNN/Tests/UnitTests/SimpleTest.cpp:13: undefined reference to `cubby_dnn::emptyOp<int>::emptyOp()'
/home/jwkim/projects/CubbyDNN/Tests/UnitTests/SimpleTest.cpp:14: undefined reference to `cubby_dnn::emptyOp<int>::print()'
collect2: error: ld returned 1 exit status
Tests/UnitTests/CMakeFiles/UnitTests.dir/build.make:100: recipe for target 'bin/UnitTests' failed
make[3]: *** [bin/UnitTests] Error 1
CMakeFiles/Makefile2:360: recipe for target 'Tests/UnitTests/CMakeFiles/UnitTests.dir/all' failed
make[2]: *** [Tests/UnitTests/CMakeFiles/UnitTests.dir/all] Error 2
CMakeFiles/Makefile2:372: recipe for target 'Tests/UnitTests/CMakeFiles/UnitTests.dir/rule' failed
make[1]: *** [Tests/UnitTests/CMakeFiles/UnitTests.dir/rule] Error 2
Makefile:229: recipe for target 'UnitTests' failed
make: *** [UnitTests] Error 2
这是一篇很长的文章..但是我很高兴听到您的任何帮助..并随时询问我任何进一步的信息