How to initialize Tensorflow on CPP project?

Hi there,
Is there any CPP functions that initialize the tensorflow?

I am trying to write a graph rewriting pass outside tensorflow, so I use TFG’s provided
test like mlir::tfg::ImportGraphDefToMlir(&context, debug_info, graphdef);. It throws the error that complains that no tensorflow Op is registered, only my custom op is registered
It is expected that the tensorflow ops is registered as well.

 $ TF_CPP_MAX_VLOG_LEVEL=4 ./test
 2021-11-23 14:50:24.049527: I tensorflow/core/framework/op.cc:130] All registered Ops:
 2021-11-23 14:50:24.049540: I tensorflow/core/framework/op.cc:132] Op<name=CustomOp1; signature=input:int32 -> output:int32>
 2021-11-23 14:50:24.049548: I tensorflow/core/framework/op.cc:132] Op<name=CustomOp2; signature=input:T -> output:T; attr=T:type>
 2021-11-23 14:50:24.049599: I tensorflow/core/framework/op.cc:80] NOT_FOUND: Op type not registered 'NoOp' in binary running on host-machine. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
 2021-11-23 14:50:24.049655: I tensorflow/core/framework/op.cc:80] NOT_FOUND: Op type not registered 'NoOp' in binary running on host-machine. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.
 2021-11-23 14:50:24.049678: F tensorflow/core/graph/graph.cc:394] Non-OK-status: status status: NOT_FOUND: Op type not registered 'NoOp' in binary running on host-machine. Make sure the Op and Kernel are registered in the binary running in this process. Note that if you are loading a saved graph which used ops from tf.contrib, accessing (e.g.) `tf.contrib.resampler` should be done before importing the graph, as contrib ops are lazily registered when the module is first accessed.

I also tried using the following test case, its output is empty.

// importer.cc
int main(int argc, char **argv) {
  ::tensorflow::port::InitMain(argv[0], &argc, &argv);
  std::vector<::tensorflow::OpDef> op_defs;

  Status s = ::tensorflow::OpRegistry::Global()->ProcessRegistrations();
  if (!s.ok()) {  VLOG(2) << " Not processed";  }

  ::tensorflow::OpRegistry::Global()->GetRegisteredOps(&op_defs);
  for (const tensorflow::OpDef &op : op_defs) {
    VLOG(2) << " Op name " << op.name();
  }
}

So, in that case, it seems that the tensorflow is not initialized , and the tensorflow ops is not registered. How could I call the initialize function?

If you are looking for new CPP function to be executed on Tensorflow. Firstly we need to register CPP op in Tensorflow. Read this guide to get more idea on how to create an op.

CPP functions are available on Tensorflow. Read for more TensorFlow C++ API Reference  |  TensorFlow Core v2.7.0