Tensorflow c++ api - c++

I recently tensorflow cpp api from a way below(on MacOS Catalina ):
1. I first Install Bazel 0.23.0
2. download tensorflow:
$ git clone https://github.com/tensorflow/tensorflow
cd tensorflow # cd to the top-level directory created
$ ./configure
bazel build //tensorflow:libtensorflow_cc.so
# build C library
$ bazel build //tensorflow:libtensorflow.so
After install I run the code below:
#include "tensorflow/core/public/session.h"
#include "tensorflow/core/framework/tensor.h"
#include "tensorflow/core/platform/env.h"
int main(){
return 0;
}
with. command below:
g++ -I/usr/local/Cellar/libtensorflow/2.1.0_1/include -L/usr/local/Cellar/libtensorflow/2.1.0_1/lib -ltensorflow -ltensorflow_cc -ltensorflow_framework -std=c++11 main.cc -o main.o
but I get Error below:
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:17:2: error:
This file was generated by an older version of protoc which is
#error This file was generated by an older version of protoc which is
^
/usr/local/Cellar/libtensorflow/2.1.0_1/include/tensorflow/core/framework/device_attributes.pb.h:18:2: error:
incompatible with your Protocol Buffer headers. Please
#error incompatible with your Protocol Buffer headers. Please
I used libprotoc 3.11.4.
what should I do? what is wrong?

Related

C++ writing JSON with JSONCPP

I was trying to use the JSONCPP library to work with JSON documents in C++.
Reading another question here, I saw this example and tried to implement:
#include "json/json.h"
int main(){
Json::Value event;
Json::Value vec(Json::arrayValue);
vec.append(Json::Value(1));
vec.append(Json::Value(2));
vec.append(Json::Value(3));
event["competitors"]["home"]["name"] = "Liverpool";
event["competitors"]["away"]["code"] = 89223;
event["competitors"]["away"]["name"] = "Aston Villa";
event["competitors"]["away"]["code"] = vec;
std::cout << event << std::endl;
}
But I keep getting this error:
json2.cpp:(.text+0x1c): referência não definida para "Json::Value::Value(Json::ValueType)"
/usr/bin/ld: json2.cpp:(.text+0x30): referência não definida para "Json::Value::Value(Json::ValueType)"
json2.cpp is the file's name, as it looks like.
Being a /usr/bin/ld a link error, I tried to add as parameter the directory path to the compile command but the error persists
Does anyone know how to fix it?
It’s all about how you build/install the library and specify the headers in the source. Since you didn’t mention what method you took to build the library, I’ll demonstrate three solutions I got to work on Ubuntu 20.04 LTS.
Install the Package
Most straightforward way is to install via apt:
$ sudo apt-get install libjsoncpp-dev
The header files will be installed to /usr /include/jsoncpp/json and can be included in json2.cpp as:
#include <jsoncpp/json/json.h>
And compile by adding the linker flag as:
$ g++ json2.cpp -o json2 -ljsoncpp
Amalgamated Source
Inside of the top-level directory of the jsoncpp repository, you can amalgamate the source by:
$ python amalgamate.py
By default, this will generate all necessary source and header files in the dist/ directory, and there is no need to link the library. Include in json2.cpp as:
#include <json/json.h>
As indicated in the build instructions, jsoncpp.cpp will have to be incorporated into your project:
$ g++ json2.cpp dist/jsoncpp.cpp -Idist -o json2
Cmake
From the top-level directory of the jsoncpp repository:
$ mkdir build
$ cd build
$ cmake -DCMAKE_BUILD_TYPE=release -DBUILD_STATIC_LIBS=ON -DBUILD_SHARED_LIBS=OFF -DARCHIVE_INSTALL_DIR=. -G "Unix Makefiles" ..
$ make; make install
This will install the header files to /usr/local/include/json. Include in json2.cpp as:
#include <json/json.h>
And compile by adding the linker flag as:
$ g++ json2.cpp -o json2 -ljsoncpp

can not build cpp using clang pre-built binary: file wchar.h not found

I am using MacOS 10.15. Since the clang shipped with MacOS does not include clang-format. I installed another pre-built clang binary from here. I have added the binary file path to my PATH variable.
export PATH="$HOME/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin:$PATH"
I tried to compile a simple program:
#include <iostream>
int main(int argc, char *argv[]) {
std::cout << "Hello world!\n";
return 0;
}
using the following command:
clang++ hello.cpp -o hello
I got the following error:
In file included from hello.cpp:1:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iostream:37:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/ios:214:
In file included from /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/iosfwd:95:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/bin/../include/c++/v1/wchar.h:118:15: fatal error: 'wchar.h' file not found
#include_next <wchar.h>
^~~~~~~~~
1 error generated.
I found that wchar.h bundled with this pre-built package is in the following directory:
/Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1/
So I added the -I flag:
clang++ -I /Users/jdhao/tools/clang+llvm-10.0.0-x86_64-apple-darwin/include/c++/v1 hello.cpp -o hello
The error still persists.
If I use clang++ shipped with MacOS, I have no problem compiling the source code:
# the following works without any error
/usr/bin/clang++ hello.cpp -o hello
I have seen post here, here, and here, but the solutions do not apply.
You got clang-format improperly. Reset the system to the state before you installed another pre-built clang binary. Then use Homebrew to install clang-format
brew install clang-format
clang+llvm-10.0.0-x86_64-apple-darwin is not suitable to your Mac. It depends on system frameworks that are not available, so you get the error finding wchar.h in a system framework. When you install clang+llvm-10.0.0-x86_64-apple-darwin you ignore framework dependencies. Homebrew will care about dependencies.

How to install gumbo.h for C++?

I am running LXLE on an Oracle VirtualBox computer. I have successfully installed gumbo for python, but I cannot find how to install the gumbo library for C++, so that if I have a C++ file that includes gumbo.h in the headers, it will actually run.
I am trying to compile a file called countWordsInSite_iterative.cpp using the following command:
g++ countWordsInSite_iterative.cpp -o countWordsInSite -lcurl -lgumbo -g
I get the following error:
countWordsInSite_iterative.cpp:32:10: fatal error: gumbo/gumbo.h: No such file or directory
#include <gumbo/gumbo.h> // For HTML parser
How can I install the necessary files to include gumbo.h?

how to install c++ library libuv on OS X?

i want to install libuv on OS X,but when i
brew install libuv
then i write a simple demo :
#include <stdio.h>
#include <uv.h>
int main() {
uv_loop_t *loop = uv_loop_new();
printf(“Now quitting.\n”);
uv_run(loop, UV_RUN_DEFAULT);
return 0;
}
always error:
main.cc:2:10: fatal error: 'uv.h' file not found
#include <uv.h>
^
1 error generated.
Answers from here may help you...
$ g++ -luv main.cc
Or
$ g++ -o main main.cc build/Release/libuv.a -framework CoreFoundation -framework CoreServices
Yes : -I/usr/local/include helped me.
But one point add this compiler flags not in project but in sdk
If you want to use Xcode you can add libuv as a Git submodule (git submodule add https://github.com/libuv/libuv Externals/libuv) and use GYP to generate an xcodeproj for libuv (as explained in libuv's README) and add this xcodeproj to your main Xcode project.
It can be automated (for easy updating) with a simple shell script (assumes you put the libuv submodule in Externals/libuv, but can be changed):
git submodule update --init
git clone https://chromium.googlesource.com/external/gyp.git Externals/libuv/build/gyp
Externals/libuv/gyp_uv.py -f xcode
Then you'll be able to add libuv as a dependency and to the libraries to link your target to:
The last thing to do is to tell Xcode where are libuv's headers:
See this post

How to install clang in custom location from SVN, making it recognize c++ standard library, on OS X

I try to compile and use clang from svn trunk. I basically try to follow the directions here:
svn co -q http://llvm.org/svn/llvm-project/llvm/trunk llvm
svn co -q http://llvm.org/svn/llvm-project/cfe/trunk llvm/tools/clang
svn co -q http://llvm.org/svn/llvm-project/clang-tools-extra/trunk llvm/tools/clang/tools/extra
svn co -q http://llvm.org/svn/llvm-project/compiler-rt/trunk llvm/projects/compiler-rt
mkdir llvm_build_release
cd llvm_build_release
cmake -DCMAKE_BUILD_TYPE=Release -DCMAKE_INSTALL_PREFIX=$HOME/usr/local -DLLVM_TARGETS_TO_BUILD=host ../llvm
make -j12
make install
Above, I configure clang to be installed in the custom location ~/usr/local since I want to be able to play with it without changing my default environment.
I then create a simple test.cpp:
#include <iostream>
int main(int argc, const char* argv[]){
std::cout << "Hello world\n";
return 0;
}
and try to compile it:
~/usr/local/bin/clang++ test.cpp -o test
but get the error message:
test.cpp:1:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
(using the system version of clang, the same compilation works fine).
If I manually enter which standard library to use, it does work
~/usr/local/bin/clang++ -std=c++11 -stdlib=libstdc++ -I/usr/include/c++/4.2.1/ -L/usr/lib test.cpp -o test
The question is: How do I configure, compile and install clang from source so that I do not have to enter these standard library settings, but instead can just enter the ordinary ~/usr/local/bin/clang++ test.cpp -o test? I have macports installed, with its version of the standard libraries and the include files, if that helps.