Can't find libstdc++? - gcc

I built gcc 4.9.0 from source, and was also planning on building clang 3.4.2, however something seems to have gone awry with regards to libstdc++, as the clang build baled pretty quickly with the linker complaining about various undefined references from std.
Indeed, I then tried compiling and linking the trivial program:
#include <iostream>
int main() {
std::cout << 42;
}
and again hit linker errors:
/tmp/ccrptgVW.o:temp.cpp:function main: error: undefined reference to 'std::cout'
/tmp/ccrptgVW.o:temp.cpp:function main: error: undefined reference to 'std::ostream::operator<<(int)'
/tmp/ccrptgVW.o:temp.cpp:function __static_initialization_and_destruction_0(int, int): error: undefined reference to 'std::ios_base::Init::Init()'
/tmp/ccrptgVW.o:temp.cpp:function __static_initialization_and_destruction_0(int, int): error: undefined reference to 'std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
(full gcc -v output), both with my freshly-minted gcc 4.9.0 and my Ubuntu's stock gcc 4.6.3.
libstdc++.so exists, in /usr/local/lib64:
ls /usr/local/lib64/libstd*
/usr/local/lib64/libstdc++.a
/usr/local/lib64/libstdc++.so
/usr/local/lib64/libstdc++.so.6.0.20
/usr/local/lib64/libstdc++.la
/usr/local/lib64/libstdc++.so.6
/usr/local/lib64/libstdc++.so.6.0.20-gdb.py
and this directory appears in LIBRARY_PATH and as an -L argument to collect2 in the verbose gcc output.
How do I restore sanity to my system and have the linker find the shared library?

As turns out from your comment, you compiled with gcc rather than g++.
Do not take it as an error, as the compilation went fine! What's showing up is a linker error. Indeed, gcc will tell ld to link against the C standard library rather than the C++ standard one.
To solve, either go for g++ directly or pass -lstdc++.

Related

Darwin `ld` stubbornly won't find symbol that's defined in object file, while `lld` does

When using macOS's built in ld, I get the following error:
ld -arch x86_64 -platform_version macos 11.0 12.2.1 -syslibroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk -lSystem -lc++ -o /Users/blashyrk/Documents/source/nim/asserttrigger /Users/blashyrk/.cache/nim/asserttrigger_d/asserttrigger.o
Undefined symbols for architecture x86_64:
"_nlvmEHPersonality", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in asserttrigger.o
ld: symbol(s) not found for architecture x86_64
However, that symbol is a function that's defined inside that very same object file.
Using objdump on the object file, I can confirm that the function is indeed there:
objdump -t ~/.cache/nim/asserttrigger_d/asserttrigger.o | grep nlvmEH
00000000000063b0 l F __TEXT,__text _nlvmEHPersonality
And just to make sure I wasn't crazy, I decided to see for myself with Ghidra (please see attached image).
So my question is, what makes macOS's ld not find this symbol? When using LLVM's lld it works, so the symbol is defined properly.
Am I, perhaps, missing a flag? Is ld expecting the EH personality function to be a part of some external object/library?
Here's also a link to the object file, if someone wants to try it out on their end: https://www46.zippyshare.com/v/0UFxM7zD/file.html
The program (if you manage to link it successfully) should run and exit with no output.
I've gotten it to work by changing the linkage type of nlvmEHPersonality from internal to external.
For some reason, ld64 cant't find it with internal linkage even though lld can.

gcc link error depending on the input file argument position

Environment
$ lsb_release -a
Description: Ubuntu 20.04.2 LTS
$gcc --version
gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Problem
link error happens depending on the position of input file.
NG
$gcc -lsndfile -o snd-003 snd-003.c
/usr/bin/ld: /tmp/ccMmdgxh.o: in function `main':
snd-003.c:(.text+0x5c): undefined reference to `sf_open'
/usr/bin/ld: snd-003.c:(.text+0x88): undefined reference to `sf_readf_short'
/usr/bin/ld: snd-003.c:(.text+0xc4): undefined reference to `sf_close'
collect2: error: ld returned 1 exit status
OK
If you change the argument position of input file, then link error does not happen.
$gcc snd-003.c -lsndfile -o snd-003
Other info
snd-003.c is uploaded below (just FYI).
https://file.io/PA4K22CzXwsi
this problem happens on other gcc version
gcc 8.4.0
gcc 7.5.0
this error happens on other libraries too (e.x. libssl).
there is no this problem on other machines.
deiban 10.9 (gcc 8.3.0)
debian 8.3 (gcc 4.9.2)
similar problem reported long time ago, but it's not solved.
strange g++ linking behavior depending on arguments order
Question
Does anyone have some clue on what's wrong with this ? gcc ? environment ?
I want to know the root cause of this behavior.
Any comments are really appreciated.
I guess this is the expected behavior (https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_3.html):
The linker will search an archive only once, at the location where it is specified on the command line. If the archive defines a symbol which was undefined in some object which appeared before the archive on the command line, the linker will include the appropriate file(s) from the archive. However, an undefined symbol in an object appearing later on the command line will not cause the linker to search the archive again. See the -( option for a way to force the linker to search archives multiple times.

How to generate position independent code in Halide with ahead of time compilation

I'm trying out halide and want to build a small shared library on top of it. But the code generated by Halide's AOT compilation seems not being position independent. How do I link the objects generated by Halide into my shared library?
[Update] extracted a small example from my code.
Specifically I used a generator with c++:
class SimpleAddition : public Halide::Generator<SimpleAddition> {
public:
SimpleAddition() : vars(4) {}
Input<Halide::Buffer<>> lhs{"lhs"};
Input<Halide::Buffer<>> rhs{"rhs"};
Output<Halide::Buffer<>> out{"out"};
std::vector<Var> vars;
void generate() {
out(vars) = lhs(vars) + rhs(vars);
}
};
HALIDE_REGISTER_GENERATOR(SimpleAddition, simple_addition)
with helpers from CMake:
halide_library(ops SRCS ${CMAKE_CURRENT_LIST_DIR}/src/simple_addition.cc
GENERATOR_NAME simple_addition
GENERATOR_ARGS lhs.type=float32 lhs.dim=4 rhs.type=float32 rhs.dim=4 out.type=float32)
and got the following error:
/usr/bin/ld: ./genfiles/halide_rt_host/halide_rt_host.a(halide_rt_host.a.o): relocation R_X86_64_PC32 against symbol `_ZN6Halide7Runtime8Internal13custom_mallocE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
I also tried generating object by running the generator myself (without CMake wrapper) with -e o,h, and similarly with llvm bitcode followed by llc -relocation-model=pic, also Func::compile_to_object. similar error occurred when I try to link the generated object back to shared library:
/usr/bin/ld: halide_runtime_x86.o: relocation R_X86_64_PC32 against symbol `_ZN6Halide7Runtime8Internal13custom_mallocE' can not be used when making a shared object; recompile with -fPIC
/usr/bin/ld: final link failed: bad value
collect2: error: ld returned 1 exit status
I'm running this on Ubuntu 18.10. Both Halide and my code are compiled with gcc-8.2.0. LLVM/Clang-7 is the one shipped in their repository. Halide is built with simple cmake ../ && make && make install.
For compiler flags of my testing code, command used in CMake is add_libarary(mylib SHARED ...). Non-CMake build is with flags like -fPIC -shared, nothing special since I'm still learning.
Halide should be generating PIC by default (see Codegen_Internal.cpp, make_target_machine()). What architecture are you targeting? What compiler/linker are you using, with what options, etc?

arm compiler error undefined reference to `__libc_init_array'

I am trying to compile a C program which utilizes dl library to access shared object functions from a dynamic library I created.
The code is very simple and from a tutorial.
I initially compiled it on x86 platform using gcc with no issues.
I am now trying to compile it for arm platform and I am getting an error.
I tried using the following to compile it
arm-none-eabi-gcc -I/usr/arm-linux-gnueabi/include -L/usr/arm-linux-gnueabi/lib ex29.c -ldl -o ex29
I get the following error:
/usr/local/share/gcc-arm-none-eabi-toolchain/bin/../lib/gcc/arm-none-eabi/5.4.1/../../../../arm-none-eabi/lib/crt0.o: In function _start':
(.text+0xe0): undefined reference to__libc_init_array'
collect2: error: ld returned 1 exit status
I am not sure what is causing this error.
Thanks for any help.

imwrite throwing compile error in OpenCV

I have been using OpenCV for a while and also the imwrite function, but unfortunately this is not working any more.
I am running with OpenCV 2.4.3 with following sample code:
imwrite("somepath/somefile.png", myMat);
The error:
Undefined symbols for architecture x86_64:
"cv::imwrite(std::string const&, cv::_InputArray const&, std::__debug::vector<int, std::allocator<int> > const&)", referenced from:
MyProject::this_callback(int, void*) in MyProject.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The error looks somewhat familiar but i cannot determine whats wrong.
Yes, I've thought you were using XCode. I had the same problem. :)
If you change the project setup so that:
you use GNU++11 as C++ language dialect
libstdc++ (GNU C++ standard) as C++ standard library
your linking problem will go away.
I use Apple LLVM 4.1.
When I had this problem, I have tried just adding a new target to one of my old projects I knew, worked. Then I've just made that target a one-source-file program.
This must be a "magic" part of XCode as I think there was a time I could not get the same project working after a restart. :S
I did what Barnabas did and also got the issue on cvdef.h. I was getting desperate so and what I did was just changed the header file.
in /usr/local/cvdef.h line 205:
I changed
include <cstdint>
typedef std::uint32_t uint;
to:
include <tr1/cstdint>
typedef std::tr1::uint32_t uint;
based on this post
I think opencv devs should apply some changes to the code for 64bit..
use something like clang version 3.8.0 (trunk 257459), instead of gcc version 4.9.2 (MacPorts gcc49 4.9.2_1+universal) to compile
for reference, cmd: clang++ -std=c++11 `pkg-config --cflags --libs opencv` code.cpp -o code

Resources