gcc's protobuf compilation issue [duplicate] - c++

This question already has answers here:
Why does the order in which libraries are linked sometimes cause errors in GCC?
(9 answers)
Closed 6 years ago.
I am running ubuntu 16.04 with gcc.
My q.ccp file is
#include <my_messages.pb.h>
int main(int argc, char **argv)
{
google::protobuf::MyMessage* logged_msg_;
return 0;
}
command used for compilation:
g++ -m64 -Wl,-O1 -L/usr/lib/x86_64-linux-gnu /usr/local/lib/libprotobuf.a my_messages.pb.cc q.cpp -lpthread
protoc --version returns: 2.2.0
gcc --version
gcc (Ubuntu 4.8.5-4ubuntu2) 4.8.5
Copyright (C) 2015 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
compile error starts with
undefined reference to `google::protobuf::internal::ExtensionSet::Clear()
and gives undefined reference error for every function of protocol buffers.

Order of arguments to GCC matters a lot (libraries should go after object files and source files). You probably want
g++ -Wall -m64 -O1 -g -L/usr/lib/x86_64-linux-gnu -L/usr/local/lib/ \
my_messages.pb.cc q.cpp -lprotobuf -lpthread
(I would believe that -Wl,-O1 is wrong and useless, but I leave you to check that)
Take some time to read the GCC command options chapter of the documentation. You might want to (temporarily) use g++ -v instead of g++ in the command above to understand what is going on.
You probably should use GNU make for your build. See this example of Makefile for inspiration. Spend some time to read documentation of make.

Related

g++ compiling error message: /usr/bin/ld: cannot find -lssl

I followed the tutorial to connect to DolphinDB server with C++ and encounted this error message when compiling main.cpp:
$ g++ main.cpp -std=c++11 -DLINUX -DLOGGING_LEVEL_2 -O2 -I../include -lDolphinDBAPI -lssl -lpthread -luuid -L../bin -Wl,-rpath ../bin/ -o main
/usr/bin/ld: cannot find -lssl
collect2: error: ld returned 1 exit status
Note that my g++ version is above v6.2:
$ g++ --version
g++ (Ubuntu 7.4.0-1ubuntu1~18.04) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
How to solve this error?
If you want to link against OpenSSL, you need to install the development package for OpenSSL, like this:
apt install libssl-dev
It may also be possible to drop the -lssl from the linker command line. (If there was a project dependency on OpenSSL, the build would not have gotten this far because the OpenSSL header files are missing, too.)

Unrecognized Command Line Option '-stdlib=libc++' with MacPorts gcc48

Context
I'm trying to compile the package "root_numpy" which is a link between the scientific analysis software "root" and the python package "numpy". It's used as part of the root wrapper "rootpy". I get a g++ error when the following line is executed:
g++ -bundle -undefined dynamic_lookup -g -arch x86_64 -headerpad_max_install_names
-arch x86_64 build/temp.macosx-10.6-x86_64-2.7/root_numpy/src/_librootnumpy.o
-o build/lib.macosx-10.6-x86_64-2.7/root_numpy/_librootnumpy.so
-L/Users/bwells/bin/root/lib -lCore -lCint -lRIO -lNet -lHist -lGraf -lGraf3d
-lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread
-lpthread -Wl,-rpath,/Users/bwells/bin/root/lib -stdlib=libc++ -lm -ldl
-lTreePlayer
g++: error: unrecognized command line option '-stdlib=libc++'
The same problem occurs when I compile a "hello world" program with the flag:
dhcp-130-112:helloworld bwells$ g++ -stdlib=libc++ helloworld.cpp
g++: error: unrecognized command line option '-stdlib=libc++'
Without that flag, it compiles fine:
dhcp-130-112:helloworld bwells$ g++ helloworld.cpp
dhcp-130-112:helloworld bwells$ ls
a.out helloworld.cpp
My compiler version is:
dhcp-130-112:helloworld bwells$ g++ --version
g++ (MacPorts gcc48 4.8.2_2) 4.8.2
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
AKA the result of running sudo port install gcc48. My Mac OS version is 10.9.3. The code file "helloworld.cpp" is as you'd expect
dhcp-130-112:helloworld bwells$ cat helloworld.cpp
#include <iostream>
int main(void)
{
std::cout << "Hello world!" << std::endl;
return 0;
}
dhcp-130-112:helloworld bwells$
Question: From everything I can gather on the internet, the "-stdlib=..." flag is a standard part of g++. Why do I get a g++ error when including it? How can I fix this?
Note:
While manually executing the setup.py line without the problem flag works, and allows the full package to compile, I experience linking errors when I try to import the resulting package into python. I'm concerned that the g++ problem here is a symptom of a larger issue, which is why I'm trying to solve it directly.
-stdlib=libc++ is a Clang (not GCC) option and tells clang to use LLVM libc++ standard library (which is what Clang uses) rather than GNU libstdc++ (which is what GCC uses).
Since you got linking errors, it seems likely that other packages you are using were compiled with clang and libc++, which is not ABI compatible with GCC's libstdc++ (except for some low-level stuff). So you'll need to compile the package with clang and libc++ as well. Apple's Xcode comes with clang (which is probably what you'd want to use for this), and MacPorts also supplies a number of clang distributions.

g++ 4.8.1 C++ Threads, std::system_error - operation not permitted?

This is not a duplicate question, because the solutions presented are not working on my compiler. I am trying to compile and run the following example from this question.
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
I have attempted to use the solutions presented in both that original question as well as the accepted answer to this duplicate. However, although I tried all the combinations listed, and in particular tried
g++ main.cpp -o main.out -pthread -std=c++11
When I run the resulting executable, I still get
terminate called after throwing an instance of 'std::system_error'
what(): Enable multithreading to use std::thread: Operation not permitted
Aborted (core dumped)
Here is the output of g++ --version.
g++ (Ubuntu/Linaro 4.8.1-10ubuntu9) 4.8.1
Copyright (C) 2013 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Is there a different ordering or set of commands I need to use for g++ 4.8.1?
this was answered here
g++ -Wl,--no-as-needed -std=c++11 -pthread main.cpp -o main.out

Does g++ with Mountain Lion supports -msse4.2?

I'm trying to run a program which uses __builtin_popcountll function.
When I compile the code using makefile which compiles source files with command/flags as shown below:
g++ -c -Wall `pkg-config opencv --cflags` -I./include -O3 -fopenmp -msse4.2 src/Utils.cpp -o src/Utils.o
It compiles without any error / warning. However, when I try to link the object (.o) files to build an executable, I get undefined symbols error.
Here is the command:
g++ src/BoostDesc.o src/Utils.o src/main.o `pkg-config opencv --libs` -lgomp -o main
and this is the complete error:
Undefined symbols for architecture x86_64:
"___builtin_popcountll", referenced from:
__ZN9boostDesc5Utils12matchHammingERKN2cv3MatERKSt6vectorIS2_SaIS2_EERS5_IS5_INS1_6DMatchESaISA_EESaISC_EE.omp_fn.0 in Utils.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1
I looked up the man page for gcc on Apple's website here, and it suggests that the flag works and I'm assuming it should work for g++ as well. Can someone confirm or refute the possibility of using this builtin function? Thnx!
g++ --version returns this:
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
This optimization is specified either by doing: -mpopcnt or -march=corei7 to the compilation.
Take one sample:
% cat popcountl.c
int main(int argc , char** argv)
{
volatile long long x = 0xf0f0f0f0f0f0f0f0;
return __builtin_popcountll(x);
}
Turns out not to be supported in that version of g++:
~/D/e/popcountl [1]> /Applications/Xcode\ 4.6.3.app/Contents/Developer/usr/bin/g++ -mpopcnt -c popcountl.c
cc1plus: error: unrecognized command line option "-mpopcnt"
~/D/e/popcountl [1]> /Applications/Xcode\ 4.6.3.app/Contents/Developer/usr/bin/g++ -march=corei7 -c popcountl.c
popcountl.c:1: error: bad value (corei7) for -march= switch
popcountl.c:1: error: bad value (corei7) for -mtune= switch
~/D/e/popcountl> /Applications/Xcode\ 4.6.3.app/Contents/Developer/usr/bin/g++ --version
i686-apple-darwin11-llvm-g++-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Works with Xcode5 g++ though.
% g++ --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 5.0 (clang-500.2.75) (based on LLVM 3.3svn)
Target: x86_64-apple-darwin12.5.0
Thread model: posix
g++ -mpopcnt -c popcountl.cpp
dhcp-3-127:~% otool -tV popcountl.o
popcountl.o:
:
0000000000000024 popcntq %rax, %rax
It looks like the flags for this optimization are not present in the 4.6 version of the XCode supplied compilers, but are present in the 5.0 version of the XCode compilers.
Bear in mind that you're actually running the llvm compiler with a g++ front-end, so it's not actually running official g++; this is probably the underlying reason for it not working properly in this case.

What are the correct link options to use std::thread in GCC under linux?

Hi I am trying to use std::thread with G++. Here is my test code
#include <thread>
#include <iostream>
int main(int, char **){
std::thread tt([](){ std::cout<<"Thread!"<<std::endl; });
tt.join();
}
It compiles, but when I try to run it the result is:
terminate called after throwing an instance of 'std::system_error'
what(): Operation not permitted
Aborted
My compiler version:
$ g++ --version
g++ (Ubuntu/Linaro 4.6.1-9ubuntu3) 4.6.1
Copyright (C) 2011 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
What is wrong with my test code?
UPDATE: I use the following command line to compile and run my code.
$ g++ -std=c++0x test.cpp
$ ./a.out
and I tried
$ g++ -std=c++0x -lpthread test.cpp
$ ./a.out
still the same.
I think on Linux pthread is used to implement std::thread so you need to specify the -pthread compiler option.
As this is a linking option, this compiler option need to be AFTER the source files:
$ g++ -std=c++0x test.cpp -pthread
In addition to using -std=c++0x and -pthread you must not use -static.
-std=c++11 -static -pthread -Wl,--whole-archive -lpthread -Wl,--no-whole-archive works together with -static!!!
See here: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52590#c4
Here's a simple CMake file for compiling a C++11 program that uses threads:
CMakeLists.txt:
cmake_minimum_required(VERSION 2.8)
list(APPEND CMAKE_CXX_FLAGS "-pthread -std=c++11 ${CMAKE_CXX_FLAGS}")
add_executable(main main.cpp)
One way of building it is:
mkdir -p build
cd build
cmake .. && make
Try compiling this way in single command:
g++ your_prog.cpp -o your_output_binary -lpthread -std=gnu++11
You can also try C++11 instead of gnu++11. Hope this works.