如何使用C ++,CMake和Tensorflow修复“ ld:在体系结构x86_64中找不到的符号?

问题描述 投票:1回答:1

我使用TensorFlow和CMake在C ++中创建了一个测试项目。但我有一个错误:

ld: symbol(s) not found for architecture x86_64

我认为,我的CMake文件中有错误。当我尝试使用gcc tensortest.cpp -ltensorflow -o tf通过终端进行编译时,一切正常。

我有两个CMake文件。

FindTensorFlow.cmake:

# Locates the tensorFlow library and include directories.
include(FindPackageHandleStandardArgs)
unset(TENSORFLOW_FOUND)

find_path(TensorFlow_INCLUDE_DIR
        NAMES
        tensorflow
        HINTS

        /usr/local/include/tensorflow)

find_library(TensorFlow_LIBRARY
        NAMES
        libtensorflow_framework.1.14.0.dylib
        libtensorflow_framework.1.dylib
        libtensorflow_framework.dylib
        libtensorflow.1.14.0.dylib
        libtensorflow.1.dylib
        libtensorflow.dylib
        HINTS
        /usr/local/lib)

# set TensorFlow_FOUND
find_package_handle_standard_args(TensorFlow DEFAULT_MSG TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)

# set external variables for usage in CMakeLists.txt
if(TENSORFLOW_FOUND)
  set(TensorFlow_LIBRARIES ${TensorFlow_LIBRARY})
  set(TensorFlow_INCLUDE_DIRS ${TensorFlow_INCLUDE_DIR})
endif()

# hide locals from GUI
mark_as_advanced(TensorFlow_INCLUDE_DIR TensorFlow_LIBRARY)

CMakeLists.txt

cmake_minimum_required(VERSION 3.15)
project(TensorFlowTest)

set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH "${PROJECT_SOURCE_DIR}/cmake/")

find_package(TensorFlow REQUIRED)
include_directories(${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})

add_executable(TensorFlowTest main.cpp)
target_link_libraries(TensorFlowTest ${TensorFlow_INCLUDE_DIRS} ${TensorFlow_LIBRARIES})

和一个main.cpp

#include <stdio.h>
#include <tensorflow/c/c_api.h>

int main() {
    printf("Hello from TensorFlow C library version %s\n", TF_Version());
    return 0;
}

重新加载项目时,我收到CMake消息:

-- Found TensorFlow: /usr/local/include  
-- Configuring done
WARNING: Target "TensorFlowTest" requests linking to directory "/usr/local/include".  Targets may link only to libraries.  CMake is dropping the item.
-- Generating done
-- Build files have been written to: /Users/neikr/CLionProjects/TensorFlowTest/cmake-build-debug

以及编译时:

Scanning dependencies of target TensorFlowTest
[ 50%] Building CXX object CMakeFiles/TensorFlowTest.dir/main.cpp.o
[100%] Linking CXX executable TensorFlowTest
Undefined symbols for architecture x86_64:
  "_TF_Version", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [TensorFlowTest] Error 1
make[2]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make[1]: *** [CMakeFiles/TensorFlowTest.dir/rule] Error 2
make: *** [TensorFlowTest] Error 2

更新

我用make尝试了VERBOSE=1,并得到以下输出:

/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -S/Users/neikr/CLionProjects/TensorFlowTest -B/Users/neikr/CLionProjects/TensorFlowTest --check-build-system CMakeFiles/Makefile.cmake 0
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_progress_start /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/progress.marks
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/Makefile2 all
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/depend
cd /Users/neikr/CLionProjects/TensorFlowTest && /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_depends "Unix Makefiles" /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest /Users/neikr/CLionProjects/TensorFlowTest/CMakeFiles/TensorFlowTest.dir/DependInfo.cmake --color=
/Applications/Xcode.app/Contents/Developer/usr/bin/make -f CMakeFiles/TensorFlowTest.dir/build.make CMakeFiles/TensorFlowTest.dir/build
[ 50%] Linking CXX executable TensorFlowTest
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -E cmake_link_script CMakeFiles/TensorFlowTest.dir/link.txt --verbose=1
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++  -g -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.14.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/TensorFlowTest.dir/main.cpp.o  -o TensorFlowTest -Wl,-rpath,/usr/local/lib /usr/local/lib/libtensorflow_framework.1.14.0.dylib 
Undefined symbols for architecture x86_64:
  "_TF_Version", referenced from:
      _main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TensorFlowTest] Error 1
make[1]: *** [CMakeFiles/TensorFlowTest.dir/all] Error 2
make: *** [all] Error 2

而且,我尝试将message("TensorFlow_LIBRARIES: ${TensorFlow_LIBRARIES}")添加到CMake的末尾,并且得到以下消息:

TensorFlow_LIBRARIES: /usr/local/lib/libtensorflow_framework.1.14.0.dylib
c++ macos tensorflow cmake
1个回答
0
投票

基于您从message()命令的打印输出,更清楚了正在发生什么。在您的FindTensorFlow.cmake文件中,find_library()调用仅找到one库(find_library()),这是预期的行为:

一旦调用成功,将设置结果变量并将其存储在缓存中,这样就不会再搜索任何调用。

[libtensorflow_framework.1.14.0.dylib选项具有多个值时,默认情况下,此命令将一次考虑一个名称并在每个目录中搜索它。

因此,一旦NAMES找到find_library()库,就搜索停止。如果还想找到可能有助于链接错误的libtensorflow_framework,则必须使用另一个libtensorflow.dylib变量指定另一个find_library()调用。因此,将TensorFlow_的该部分更改为类似的内容应该会有所帮助:

FindTensorFlow.cmake
© www.soinside.com 2019 - 2024. All rights reserved.