tensorflow c++踩的坑

问题1:头文件找不到

  • 编译tensorflow源码时添加环境变量
    几种方式:
  1. 打开 /etc/profile 文件,在末尾添加 export PATH=(自己的路径)$PATH
  2. 把编译好的文件复制到 /usr/local/install/ 中
  • 编译时tensorflow的一些头文件找不到
    看错误提示,到相应的文件夹下看看有没有相应的文件
  1. 存在,说名环境变量没有设置正确
  2. 若不存在,看看是否存在同样文件名的 .proto 文件,使用 protoc 生成 .cpp 和 .h 文件
  3. 若也不存在 .proto文件,使用 find |grep 文件名,找到相应文件,可以复制到错误提示的文件夹中,或者把当前路径添加到环境变量中(原文件中使用的名字需要作相应的改动改动)

自己写的文件中方法找不到

ctrl + 点击,跳转到源文件中查看源码,若却是不存在,使用 grep 在源文件中搜索定义的地方,然后在自己的代码中替换,并作相应修改

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
xzp@xzp-HP-Pavilion-Notebook:/usr/local/include/tf$ grep ends_with /usr/local/include/tf -r
/usr/local/include/tf/tensorflow/docs_src/tutorials/images/image_recognition.md: if (tensorflow::StringPiece(file_name).ends_with(".png")) {
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece.h: bool ends_with(StringPiece x) const {
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece.cc: if (ends_with(x)) {
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(a.ends_with(a));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(a.ends_with("bar"));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(a.ends_with(e));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(b.ends_with(s1));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(b.ends_with(b));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(b.ends_with(e));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(e.ends_with(""));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(!a.ends_with(b));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(!b.ends_with(a));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(!e.ends_with(a));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: // ends_with
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(abc.ends_with(abc));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(!abc.ends_with("abcdefguvwxyz"));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/stubs/stringpiece_unittest.cc: EXPECT_TRUE(abc.ends_with("nopqrstuvwxyz"));
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/protobuf/src/google/protobuf/util/internal/utility.cc: return text.ends_with(suffix);
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/absl/absl/strings/match.h:// Returns whether a given std::string `text` ends with `ends_with`, ignoring case
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/googletest/googlemock/test/gmock-matchers_test.cc: Matcher<const string&> ends_with_ok = EndsWith("ok");
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/googletest/googlemock/test/gmock-matchers_test.cc: ASSERT_THAT("book", ends_with_ok);
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/googletest/googlemock/test/gmock-matchers_test.cc: EXPECT_NONFATAL_FAILURE(EXPECT_THAT(bad, ends_with_ok),
/usr/local/include/tf/tensorflow/contrib/makefile/downloads/re2/re2/stringpiece.h: bool ends_with(const StringPiece& x) const {
xzp@xzp-HP-Pavilion-Notebook:/usr/local/include/tf$

protoc 命令使用踩过的坑

  1. 版本不正确
    把目前使用的protoc 卸载,然后安装相应的版本,如果使用 apt remove protoc 卸载失败,可以直接在/usr/local/include/ 中把 头文件文件夹删了,在 /usr/local/bin/ 中把 protoc 删除
  2. 找不到依赖的 .proto 文件,使用 protoc -I=相应路径 这种方式(如:sudo protoc -I=/usr/local/include/tf/ -I=. –cpp_out=. cluster.proto )