将TensorFlow链接到C ++。 Protobuf(/ usr / local / include / google / protobuf)

时间:2017-08-24 12:42:33

标签: c++ tensorflow

系统信息

  • 我是否编写了自定义代码:自定义项目
  • 操作系统平台和分发:macOS Sierra(10.12.6)
  • 安装的TensorFlow:来源
  • TensorFlow版:Git标记为1.3,掌握d27ed9c
  • Python版:Python 2.7.13 :: Anaconda 4.4.0
  • Bazel版本:0.5.3_1-homebrew
  • Protobuf版本:3.3.2-homebrew
  • CUDA / cuDNN版本:N / A
  • GPU型号和内存:N / A
  • 重现的确切命令

问题描述

我为Python&编译了TensorFlow源代码。遵循http://www.blitzblit.com/2017/06/11/creating-tensorflow-c-headers-and-libraries/中显示的步骤的C ++ API绑定(我们必须考虑到本教程未更新)。编译TF源代码后,我在C ++项目中包含了TF库:

#include <tensorflow/core/public/session.h>

最后,我编译了C ++项目(直到这里一切似乎都很好)。然后,当我尝试启动C ++代码时,我收到以下消息:

/Applications/CLion.app/Contents/bin/cmake/bin/cmake --build /Users/arcadillanzacarmona/Desktop/FaceSDK/    cmake-build-debug --target FaceSDK -- -j 2
[ 96%] Built target dlib
[ 96%] Building CXX object CMakeFiles/FaceSDK.dir/main.cpp.o
clang: warning: -lcurl: 'linker' input unused [-Wunused-command-line-argument]
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:328:33: error: expected a qualified name after 'typename'
template <typename It, typename VoidPtr> class RepeatedPtrOverPtrsIterator;
                                ^
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:328:33: error: expected ',' or '>' in template-parameter-list
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:864:59: error: template argument for non-type template     parameter must be an expression
  typedef internal::RepeatedPtrOverPtrsIterator<Element*, void*>
                                                          ^~~~~
/usr/local/include/google/protobuf/repeated_field.h:328:33: note: template parameter is declared here
template <typename It, typename VoidPtr> class RepeatedPtrOverPtrsIterator;
                                ^
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:867:55: error: template argument for non-type template     parameter must be an expression
                                                const void* const>
                                                      ^~~~~
/usr/local/include/google/protobuf/repeated_field.h:328:33: note: template parameter is declared here
template <typename It, typename VoidPtr> class RepeatedPtrOverPtrsIterator;
                                ^
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:2258:38: error: expected a qualified name after 'typename'
template <typename Element, typename VoidPtr>
                                     ^
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:2258:38: error: expected ',' or '>' in template-parameter-list
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:2262:48: error: template argument for non-type template     parameter must be an expression
  typedef RepeatedPtrOverPtrsIterator<Element, VoidPtr> iterator;
                                               ^~~~~~~
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^~~~~~
/usr/local/include/google/protobuf/repeated_field.h:2258:38: note: template parameter is declared here
template <typename Element, typename VoidPtr>
                                     ^
/opt/local/include/gif_lib.h:286:17: note: expanded from macro 'VoidPtr'
#define VoidPtr void *
                ^
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:22:
In file included from /usr/local/include/tensorflow/core/framework/device_attributes.pb.h:29:
/usr/local/include/google/protobuf/repeated_field.h:2289:61: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  bool operator==(const iterator& x) const { return it_ == x.it_; }
                                                           ~^~~~
/usr/local/include/google/protobuf/repeated_field.h:2290:61: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  bool operator!=(const iterator& x) const { return it_ != x.it_; }
                                                           ~^~~~
/usr/local/include/google/protobuf/repeated_field.h:2293:59: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  bool operator<(const iterator& x) const { return it_ < x.it_; }
                                                         ~^~~~
/usr/local/include/google/protobuf/repeated_field.h:2294:61: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  bool operator<=(const iterator& x) const { return it_ <= x.it_; }
                                                           ~^~~~
/usr/local/include/google/protobuf/repeated_field.h:2295:59: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  bool operator>(const iterator& x) const { return it_ > x.it_; }
                                                         ~^~~~
/usr/local/include/google/protobuf/repeated_field.h:2296:61: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  bool operator>=(const iterator& x) const { return it_ >= x.it_; }
                                                           ~^~~~
/usr/local/include/google/protobuf/repeated_field.h:2324:70: error: member reference base type 'const iterator' (    aka 'const int') is not a structure or union
  difference_type operator-(const iterator& x) const { return it_ - x.it_; }
                                                                    ~^~~~
In file included from /Users/arcadillanzacarmona/Desktop/FaceSDK/main.cpp:33:
In file included from /usr/local/include/tensorflow/core/public/session.h:24:
In file included from /usr/local/include/tensorflow/core/framework/tensor.h:21:
In file included from /usr/local/include/tensorflow/core/framework/allocator.h:26:
In file included from /usr/local/include/tensorflow/core/framework/variant.h:30:
In file included from /usr/local/include/tensorflow/core/platform/mutex.h:31:
/usr/local/include/tensorflow/core/platform/default/mutex.h:25:10: fatal error: 'nsync_cv.h' file not found
#include "nsync_cv.h"
         ^
15 errors generated.
gmake[3]: *** [CMakeFiles/FaceSDK.dir/build.make:63: CMakeFiles/FaceSDK.dir/main.cpp.o] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:68: CMakeFiles/FaceSDK.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:80: CMakeFiles/FaceSDK.dir/rule] Error 2
gmake: *** [Makefile:118: FaceSDK] Error 2

任何人都可以帮助我吗?似乎主要问题出在谷歌/ protobuf。

2 个答案:

答案 0 :(得分:0)

我有类似的问题,通过改变编译器和使用不同版本的C ++(即从clang到g ++ - 5,g ++ - 7等)来解决。我建议您可以尝试以这种方式解决它。很抱歉无法提供直接解决方案。

对于nsync_cv的错误, alc1218 对此post的解决方案可能会有所帮助,这将更改文件mutex.h,如下所示:

/* Copyright 2015 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#ifndef TENSORFLOW_PLATFORM_DEFAULT_MUTEX_H_
#define TENSORFLOW_PLATFORM_DEFAULT_MUTEX_H_

// IWYU pragma: private, include "third_party/tensorflow/core/platform/mutex.h"
// IWYU pragma: friend third_party/tensorflow/core/platform/mutex.h

#include <chrono>
#include <condition_variable>
#include <mutex>
#include "tensorflow/core/platform/thread_annotations.h"
namespace tensorflow {

#undef mutex_lock

enum LinkerInitialized { LINKER_INITIALIZED };

// A class that wraps around the std::mutex implementation, only adding an
// additional LinkerInitialized constructor interface.
class LOCKABLE mutex : public std::mutex {
 public:
  mutex() {}
  // The default implementation of std::mutex is safe to use after the linker
  // initializations
  explicit mutex(LinkerInitialized x) {}

  void lock() ACQUIRE() { std::mutex::lock(); }
  bool try_lock() EXCLUSIVE_TRYLOCK_FUNCTION(true) {
    return std::mutex::try_lock();
  };
  void unlock() RELEASE() { std::mutex::unlock(); }
};

class SCOPED_LOCKABLE mutex_lock : public std::unique_lock<std::mutex> {
 public:
  mutex_lock(class mutex& m) ACQUIRE(m) : std::unique_lock<std::mutex>(m) {}
  mutex_lock(class mutex& m, std::try_to_lock_t t) ACQUIRE(m)
      : std::unique_lock<std::mutex>(m, t) {}
  mutex_lock(mutex_lock&& ml) noexcept
      : std::unique_lock<std::mutex>(std::move(ml)) {}
  ~mutex_lock() RELEASE() {}
};

// Catch bug where variable name is omitted, e.g. mutex_lock (mu);
#define mutex_lock(x) static_assert(0, "mutex_lock_decl_missing_var_name");

using std::condition_variable;

inline ConditionResult WaitForMilliseconds(mutex_lock* mu,
                                           condition_variable* cv, int64 ms) {
  std::cv_status s = cv->wait_for(*mu, std::chrono::milliseconds(ms));
  return (s == std::cv_status::timeout) ? kCond_Timeout : kCond_MaybeNotified;
}

}  // namespace tensorflow

#endif  // TENSORFLOW_PLATFORM_DEFAULT_MUTEX_H_

或者只是找到“nsync_cv”的位置并包含整个路径。

答案 1 :(得分:0)

问题是gif_lib.h和protobuf头文件不兼容。更具体地说,问题是gif_lib.h使用预处理器来定义名为VoidPtr的宏,然后protobuf头使用VoidPtr作为标识符。这个错误与gif_lib紧密相关,而不是protobuf!按照惯例,预处理器宏通常是大写的,应该包含库名,如果可能的话应该完全避免(在这种情况下,不会有一个typedef吗?)。

以下是一些可能的解决方法:

  • gif_lib.h之前包含protobuf标头(或包含它的东西,如TensorFlow标头)(或者,包括它的东西)。
  • 在包含gif_lib.h之后,将#undef VoidPtr写在另一行(但这可能会导致其他神秘问题,具体取决于gif_lib.h的实施方式。
  • 不要#include来自任何一个文件的那两个标题;把它们藏在你自己的界面后面。
  • 向编写gif_lib的人提出错误,让他们以危险的方式停止使用预处理器。
  • 使用设计更佳的库代替gif_lib