How to compile Google Protobuf command line interface compile - c++

I am trying to raw decode a protobuf binary. I installed the google protobuf library from the source code https://github.com/google/protobuf
I am able to use the command line to decode raw a protobuf binary using the command protoc --decode_raw <encodedfile>. I want to be able to do this programmatically using the c++ library. Something similar to the following example in the documentation.
https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.compiler.command_line_interface
However trying to compile a simple piece of code does not work.
#include <iostream>
#include <fstream>
#include <string>
#include <google/protobuf/compiler/command_line_interface.h>
using namespace google::protobuf::compiler;
using namespace std;
int main(int argc, char* argv[]) {
google::protobuf::compiler::CommandLineInterface cli_;
cerr << "Compiled And Run" << endl;
}
Compile command
c++ my_program.cc -o my_program -pthread -I/usr/local/include -pthread -L/usr/local/lib -lprotobuf -lpthread
I see the following error
my_program.cc:(.text+0x24): undefined reference to `google::protobuf::compiler::CommandLineInterface::CommandLineInterface()'
my_program.cc:(.text+0x4f): undefined reference to `google::protobuf::compiler::CommandLineInterface::~CommandLineInterface()'
my_program.cc:(.text+0x70): undefined reference to `google::protobuf::compiler::CommandLineInterface::~CommandLineInterface()'
Appreciate any help with this.

Protobuf compiler is in a different library, libprotoc. You need to link against it
c++ my_program.cc -o my_program -pthread -I/usr/local/include -pthread -L/usr/local/lib -lprotoc -lprotobuf -lpthread
Note that -lprotoc needs to appear before -lprotobuf, because libprotoc uses functions of libprotobuf.

Related

Boost filesystem incompatible with c++11 threads

I've had a maddening problem that I need some help with. I'm attempting to write a file finder or parser that finds all of the files of a given format in a directory. I want this to be a class, and I also want it to run in it's own separate thread from main(). I am using Ubuntu 14.04LTS, with it's boost installation (1.54). Here is the vanilla version of my code, just linking against boost::system.
#include <iostream>
#include <thread>
#include <vector>
#include <string>
class fileFinder {
private:
std::string dName;
public:
fileFinder() : dName("") { };
fileFinder(const std::string &dirName) : dName(dirName) { };
void runFileFinder(void) {
std::string fileFinderName = "Hi from filefinder!";
std::cout << fileFinderName << std::endl;
};
};
int main(int argc, char *argv[]) {
//Get the dirname, not safe yet
std::string dirName = argv[1];
fileFinder fFinderThread(dirName);
std::thread t1(&fileFinder::runFileFinder, &fFinderThread);
t1.join();
return EXIT_SUCCESS;
}
When I compile, everything works out great, and the class gets instantiated, and then run in a separate thread. I will link against boost_system just to show that everything is still ok.
> g++ -g -Wall -I/usr/include/boost/ -c rFileFinder.cpp -std=c++11 -pthread
> g++ -g -Wall rFileFinder.o -o rFileFinder -L/usr/lib/x86_64-linux-gnu/ -lboost_system -std=c++11 -pthread
> ./rFileFinder abcd
Hi from filefinder: abcd
Now, since I want to find all the files of a certain type, using boost::filesystem would be awesome. Even attempting to link against the boost::filesystem library produces a runtime threading error (just add -lboost_filesystem to the libraries).
> g++ -g -Wall -I/usr/include/boost/ -c rFileFinder.cpp -std=c++11 -pthread
> g++ -g -Wall rFileFinder.o -o rFileFinder -L/usr/lib/x86_64-linux-gnu/ -lboost_system -lboost_filesystem -std=c++11 -pthread
> ./rFileFinder abcd
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread. Operation not permitted
Aborted (core dumped)
And so, this is driving me insane, since I am going to need to have both multithreading capabilities (there is a lot more than just this part of the problem). I've attempted to tease this answer out of the internet, but basically everything I come across is about how the linking to c++11 or pthread isn't done correctly in the compilation and linker steps. Is there a way for me to use both std::thread and boost::filesystem, or am I just hosed?
You need to link to the posix threads library on your system
With gcc or clang, typically this is done by supplying g++ -pthread on the command line.
Technically you should specify to link with the respective dynamic library too
g++ -pthread test.cpp -lboost_system -lboost_thread -lboost_filesystem
Using g++ 4.8, it seems that one cannot have both the -pthread and -std=c++11 flags for the compiler options. They mutually exclude each other on mac (with homebrew gcc48) and on my Ubuntu distro (14.04LTS with g++-4.8). Just use -std=c++11.

Compiling issues with boost

I'm having problems with compiling a program which includes "boost/asio.hpp".
Compiling this program(taken from boost site):
example.cpp:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
with
c++ -I path/to/boost_1_55_0 example.cpp -o example
works fine.
But when the program includes:
boost/asio.hpp
And I'm trying to compile it with:
g++ -I /usr/local/boost_1_55_0 example.cpp -o example -lboost_system -lboost_thread
an executable is generated ,but I'm getting this error when trying to execute "example":
./example: error while loading shared libraries: libboost_system.so.1.55.0: cannot open shared object file: No such file or directory
The file "libboost_system.so.1.55.0" is located at "/usr/local/lib".
I also tried to compile the program with :
g++ -I /usr/local/boost_1_55_0 -L/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
And got the same error.
How can I fix this?
You need to tell the linker where to find the library it needs. I prefer RPATH for this:
g++ -I /usr/local/boost_1_55_0 -Wl,-rpath=/usr/local/lib example.cpp -o example -lboost_system -lboost_thread
That bakes /usr/local/lib into the executable so ld can find it later. You can see what ld will load by running ldd example after building. I bet right now it says "not found" and after adding RPATH it will find the library.
Another option is to set /usr/local/lib as a system search path in your /etc/ld.so.conf, but that's quite a bit more heavyweight.
set up LD_LIBRARY_PATH as export LD_LIBRARY_PATH= path to boost

C++ / mysql Connector - undefined reference to get_driver_instance - already tried the easy stuff

Yes this question has been asked before ... I've tried everything mentioned in the previous answers. My setup is really straightforward so this shouldn't be so hard.
I just want to program against mysql using C++. My source code is taken verbatem from the 'hello world' type example here:
http://dev.mysql.com/doc/refman/5.1/en/connector-cpp-examples-complete-example-1.html
I am on Ubuntu 12.10. I am trying:
g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18 -L/usr/lib/mysqlcppconn -lmysqlcppconn firsttry.cpp
It compiles (if I use -c option) but won't build, giving me the infamous:
/tmp/ccn768hj.o: In function `main':
firsttry.cpp:(.text+0x3a): undefined reference to `get_driver_instance'
A few details:
'firsttry.cpp' is just what I named the source code file, again taken verbatem from the official example
As you can see I AM linking in the mysqlclient library and the mysqlcppconn library. Many times when this question has been asked previously, the answer was to link those.
Some other historical answers suggest the sample source code is wrong and that the function in question needs to be in the sql::mysql namespace etc. I am pretty sure the source code is fine. Again, it compiles, and changing the namespaces in the source code just seems to make it worse.
Thank you in advance for any help you can provide.
So I have now had this problem for a week now and I became very frustrated with it as well. I just now was able to finally build a program that does nothing except login to mysql and I literally squealed with joy. Here is what I have and I hope it helps.
I first compiled the c++ connector library from source but after a while I thought maybe I did something wrong so I then just used apt to get it with:
sudo apt-get install libmysqlcppconn-dev
And here is my simple tester source file "tester.cpp"
#include <stdlib.h>
#include <iostream>
#include <mysql_connection.h>
#include <driver.h>
#include <exception.h>
#include <resultset.h>
#include <statement.h>
using namespace sql;
int main(void){
sql::Driver *driver;
sql::Connection *con;
driver = get_driver_instance();
con = driver->connect("tcp://127.0.0.1:3306","root","YOURPASSWORD");
return 0;
}
And finally g++ compile command:
g++ -Wall -I/usr/include/cppconn -o testapp tester.cpp -L/usr/lib -lmysqlcppconn
This worked for me and I hope it helps you solve your problem!
For me simply swapping the order of the last two arguments fixed this problem. I don't know why but the linker is able to find the function get_driver_instance if I specify the -lmysqlcppconn option at the end after the source file.
g++ -Wall -o firsttry_prog -I/usr/include/mysqlcppconn -L/usr/lib/mysqlcppconn firsttry.cpp -lmysqlcppconn
Also note that I took out the following options as I think they are redundant
-I/usr/local/boost_1_53_0 -L/usr/lib/x86_64-linux-gnu -l:libmysqlclient_r.so.18
In case you are as forgetful as me and didn't link the library in CMakeLists.txt:
target_link_libraries(<target> mysqlcppconn)
If all the paths are included throw param -I. You would see whether there is a problem if you compile like this:
g++ -g -o0 -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g -o0 -L/usr/local/lib -L/usr/local/mysql/lib -lmysqlcppconn main.o -o test
the problem will appear:
main.o: In function `main':
/home/huangxw/workspace/public/soal/test/main.cpp:165: undefined reference to `get_driver_instance'
collect2: ld returned 1 exit status
Now you must adjust the order of -lmysqlcppconn and main.o:
g++ -g -o0 -I/usr/local/include -I/usr/local/boost/include -c main.cpp -o main.o
g++ -g -o0 -L/usr/local/lib -L/usr/local/mysql/lib main.o -o test -lmysqlcppconn
That is all!!
The reason is simple. You can find out using the web or ask me to elaborate.

undefined reference to `png_read_info'

Im trying to compile source code, but receive:
undefined reference topng_read_info'
File header:
#include "PngImage.hh"
#include <png.h>
#include <cstdio>
#include <csetjmp>
#include <cstring>
My compiler settings:
-I/usr/include/libxml2 -I/usr/include/osmpbf -O0 -g3 -Wall -c -fmessage-length=0 -lpng
What I`m doing wrong ?
Update:
I`m trying linking:
-I/usr/include/libxml2 -I/usr/include/osmpbf -O0 -g3 -Wall -c -fmessage-length=0 -L/usr/include/libpng -lpng
Library libpng already installed. Nothing change I receive again:
undefined reference topng_read_info
Fixed. My great mistake. I set compiler settings in eclipse instead of linker settings. Thank all for answers
You are not linking against libpng. Add -L/path/to/dir_containing/libpng -lpng to your link step.
you get "undefined reference to" issue when the compiler cant find the link to an object so you miss an include or setting library path so try what greg said and check if you miss some include

Compilation problem in the standard x86_64 libraries

I am having trouble compiling a program I have written. I have two different files with the same includes but only one generates the following error when compiled with g++
/usr/lib/gcc/x86_64-linux-gnu/4.4.1/../../../../lib/crt1.o: In function `_start':
/build/buildd/eglibc-2.10.1/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main'
collect2: ld returned 1 exit status
The files I am including in my header are as follows:
#include <google/sparse_hash_map>
using google::sparse_hash_map;
#include <ext/hash_map>
#include <math.h>
#include <iostream>
#include <queue>
#include <vector>
#include <stack>
using std::priority_queue;
using std::stack;
using std::vector;
using __gnu_cxx::hash_map;
using __gnu_cxx::hash;
using namespace std;
Searching the internet for those two lines hasn't resulted in anything to help me. I would be very grateful for any advice. Thank you
To build two separate programs you need both source files to define main() function.
To build a single program out of two source files - first compile each file with -c options (compile only) - you will get two .o files, then link these files together. Something like this:
$ g++ -Wall -pedantic -ggdb -O -c -o module0.o module0.cpp
$ g++ -Wall -pedantic -ggdb -O -c -o module1.o module1.cpp
$ g++ -Wall -pedantic -ggdb -O -o prog module0.o module1.o
to build binary prog from two source files.
If you need to link with some library, you'll have to point compiler to it's headers with -I and to objects with -L flags, then tell the linker to actually reference the library with -l.
Hope this helps.
You need a main function and you don't have one. If you do have a main function, show more code please.
It looks like main is not defined. Do you have one defined for your second program? Can you post more details about the source body that fails to link?