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.
Related
I am working with some code that requires g++12 I could not find a better way to install g++ 12 on Ubuntu 20.4.4 so I installed it using homebrew. When I run g++-12 --version I get
g++-12 (Homebrew GCC 12.2.0) 12.2.0 Copyright (C) 2022 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.
When I try to compile any program using g++-12 I get the error
/usr/bin/ld: cannot find -lm collect2: error: ld returned 1 exit
status
I have tried adding a -L and -I flags that point to lib and include folders of the homebrew install of g++ 12 but this does not seem to help.
g++-12 -std=gnu++20 main.cpp track.cpp -o track
g++-12 -o hello HelloWorld.cpp
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.)
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.
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.
I am trying to convert some gpl code(not mine!) from C++ to C so I can use it in a c-only project. I read about llvm and got excited but I am running into a linker error that I have no idea how to fix:
the error is:
Intrinsic prototype has incorrect number of arguments!
void (i8*, i8*, i64, i32, i1)* #llvm.memmove.p0i8.p0i8.i64
Broken module found, compilation aborted!
Stack dump:
0. Running pass 'Function Pass Manager' on module 'ld-temp.o'.
1. Running pass 'Module Verifier' on function '#_ZNSt15__copy_backwardILb1ESt26random_access_iterator_tagE8__copy_bIxEEPT_PKS3_S6_S4_'
Anyone ever see this before? Any idea what could cause it?
Here is the commands I used to get this far:
dhcp-100-140:src joeltucci$ llvm-g++ msb.C -c -emit-llvm
dhcp-100-140:src joeltucci$ llvm-g++ rabincmd.C -c -emit-llvm
In file included from rabincmd.C:27:
rabinpoly.h: In member function ‘u_int64_t rabinpoly::append8(u_int64_t, u_char) const’:
rabinpoly.h:56: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘long int’
rabincmd.C: In function ‘void printChunkContents(u_int64_t, const unsigned char*, int)’:
rabincmd.C:91: warning: too few arguments for format
rabincmd.C: In member function ‘virtual void ExtractChunkProcessor::internalCompleteChunk(u_int64_t, u_int64_t)’:
rabincmd.C:547: warning: deprecated conversion from string constant to ‘char*’
rabincmd.C:551: warning: deprecated conversion from string constant to ‘char*’
dhcp-100-140:src joeltucci$ llvm -ld m
mkrabincmd.sh msb.C msb.o
mkverboseRabin.sh msb.h
dhcp-100-140:src joeltucci$ llvm -ld *.o -o program
-bash: llvm: command not found
dhcp-100-140:src joeltucci$ llvm-ld *.o -o program
-bash: llvm-ld: command not found
dhcp-100-140:src joeltucci$ llvm-
llvm-cpp-4.2 llvm-g++ llvm-g++-4.2 llvm-gcc llvm-gcc-4.2
dhcp-100-140:src joeltucci$ llvm-g++ *.o -o program
collect2: ld terminated with signal 6 [Abort trap]
Intrinsic prototype has incorrect number of arguments!
void (i8*, i8*, i64, i32, i1)* #llvm.memmove.p0i8.p0i8.i64
Broken module found, compilation aborted!
Stack dump:
0. Running pass 'Function Pass Manager' on module 'ld-temp.o'.
1. Running pass 'Module Verifier' on function '#_ZNSt15__copy_backwardILb1ESt26random_access_iterator_tagE8__copy_bIxEEPT_PKS3_S6_S4_'
dhcp-100-140:src joeltucci$ llvm-gcc --version
i686-apple-darwin10-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2332.3)
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.
dhcp-100-140:src joeltucci$
dhcp-100-140:src joeltucci$
dhcp-100-140:src joeltucci$
dhcp-100-140:src joeltucci$ llvm-g++ *.o -o program
llvm-g++ llvm-g++-4.2
dhcp-100-140:src joeltucci$ llvm-gcc-4.2 --version
i686-apple-darwin10-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2332.3)
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.
Well your problem is probably a line up
llvm -ld *.o -o program -bash: llvm: command not found dhcp-100-140:src
The error you list is most likely related to llvm -ld not being found, therefore you're missing some needed files in the next command..... did you mean llvm-ld instead?