I have linking errors, when trying to do an atomic load of a 16 byte block. I have the following code:
#include <atomic>
struct MyStruct{
long x; long y;
};
struct X{
std::atomic<MyStruct> myStruct;
};
int main(){
X x;
MyStruct s = atomic_load(&x.myStruct);
}
When I compile this with (g++ version 5.3.1):
g++ --std=c++11 test.cpp
I get the error
/tmp/ccrvzLMq.o: In function `std::atomic<MyStruct>::load(std::memory_order) const':
test.cpp:(.text._ZNKSt6atomicI8MyStructE4loadESt12memory_order[_ZNKSt6atomicI8MyStructE4loadESt12memory_order]+0x1c): undefined reference to `__atomic_load_16'
collect2: error: ld returned 1 exit status
If (following a hint in another post) I add the "-latomic" flag, I get the error "/bin/ld: cannot find /usr/lib64/libatomic.so.1.1.0". And indeed that file does not exist.
Any suggestions?
Gavin
Related
Simply including the OpenCV header results in linking error. Why is that?
// test.cpp
#include <opencv2/opencv.hpp>
int foo();
int bar();
int main() {
}
If I compile the file with g++ test.cpp, the following linking error occurs:
/tmp/ccugmQl4.o: In function `cv::String::~String()':
test.cpp:(.text._ZN2cv6StringD2Ev[_ZN2cv6StringD5Ev]+0x14): undefined reference to `cv::String::deallocate()'
/tmp/ccugmQl4.o: In function `cv::String::operator=(cv::String const&)':
test.cpp:(.text._ZN2cv6StringaSERKS0_[_ZN2cv6StringaSERKS0_]+0x28): undefined reference to `cv::String::deallocate()'
collect2: error: ld returned 1 exit status
If I compile with g++ test.cpp -lopencv_core, it works all right.
My question is:
It seems to me that there's no need to resolve undefined symbols if I do not use it, like the functions foo and bar. There's no definition for them but the compile-link process works alright.
I don't use any OpenCV functions either. Why is there linking error only for OpenCV functions?
And what kinds of stuff defined in headers can cause such a linking error?
If you tweak your example a little bit
// test.cpp
int foo();
int bar() {
foo();
}
int main() {
}
You would notice that it'd stop working because linker won't be able to understand what is foo();
The same thing happens when you include opencv header - there are references to functions which are declared but since you never link opencv itself - linker can't figure what those functions are and where to get them.
I am currently learning C++ and was learning how to deal with multiple files when I came across this compiler error:
" In function main':
bravo.cpp:(.text+0x71): undefined reference tosum(int, int)'
collect2: error: ld returned 1 exit status "
The code i was building was:
main function
#include<iostream>
#include "add.h"
int main(){
int a,b;
std::cout<<"Give me two numbers to add: \n";
//std::cin>>a>>b;
std::cout<<"The sum of two numbers is "<< sum(10,5)<< std::endl;
}
header file
#pragma once
int sum(int a, int b);
Sum function
#include<iostream>
#include "add.h"
int sum(int a, int b){
return a+b;
}
So where did i go wrong?
I tried checking the error on stack overflow but i wasn't able to implement the solution on my problem?
Any ideas?
Thanks.
You are receiving a linker error because you likely just tried to compile your main.cpp function. The linker can then not find the sum(int, int), since you do not compile the add.cpp function as well.
Assuming you are using g++, you can compile both files like this:
g++ main.cpp add.cpp
and it will work.
While trying to build my small test I encounter an error I don't understand why it shouldn't work. I'm using Eclipse and Cygwin.
The Header and the Source files are separated into different Folders and I put them also into the Cygwin include folders.
Console log
16:05:41 **** Incremental Build of configuration Debug for project Testarea ****
make all
Building target: Testarea.exe
Invoking: Cygwin C++ Linker
g++ -o "Testarea.exe" ./Source/lint.o ./Source/tester.o
./Source/tester.o: In function `main':
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4: undefined reference to `lint::lint()'
/cygdrive/d/CWork/Testarea/Debug/../Source/tester.cpp:4:(.text+0x20): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `lint::lint()'
collect2: error: ld returned 1 exit status
make: *** [makefile:47: Testarea.exe] Error 1
16:05:41 Build Finished (took 348ms)
tester.cpp
#include <iostream>
#include <lint.h>
int main(){
lint* a = new lint();
std::cout << "hallo";
return 0;
}
lint.cpp
class lint{
private:
int* a;
public:
lint(){
a = new int();
};
lint(int b){
a = new int(b);
};
lint(lint& b){
a = new int(b.value());
};
int value(){
return *a;
};
};
lint.h
#ifndef HEADER_LINT_H_
#define HEADER_LINT_H_
class lint{
public:
lint();
lint(int b);
lint(lint& b);
int value();
};
#endif
Your problem is that you've got 2 classes there. One called lint and the other called lint but only available in the lint.cpp file.
Implementations are done:
#include "lint.h"
lint::lint() {}
and so forth.
Please help me
Error details as follows:
src/a.cpp:2972: undefined reference to `B::XTT collect2: ld returned 1 exit status
B::XTT is a static constant variable in the B struct.
2969 if (index != B::XTT) {
2970 index_map_iter = lookup_index.find(merchandising_index);
2971 merchandising_index = index_map_iter != output_index.end() ? \
2972 index_map_iter->second : B::XTT;
It is very strange that the first time I use B::XTT on line 2969, it does not produce an error. But at 2972 I use B::XTT, and it produces an error.
And if I add -O2 option when I use g++ command, it does not produce an error.
But it does produce an error without -O2.
When you declare a static const member variable
class foo {
// omitted other members
static int MyConst;
};
you have to define it in one compilation unit.
int foo:MyConst = 42;
I am just trying to make a vector, but it gives me a huge error and I am following a working example from my other project. The code:
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
struct organism {
bool One;
bool Two;
};
std::vector<organism> organisms;
int main() {
printf("Content-type: text/html\n\n");
printf("TEST");
printf(getenv("QUERY_STRING"));
return 0;
}
The error:
> "make"
C:/MinGW/bin/gcc.exe -o build/e2.exe source/main.cpp
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.text$_ZN9__gnu_cxx13new_allocatorI8organismE10deallocateEPS1_j[__gnu_cxx::new_allocator<organism>::deallocate(organism*, unsigned int)]+0xd): undefined reference to `operator delete(void*)'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt12_Vector_baseI8organismSaIS0_EED2Ev+0x13): undefined reference to `__gxx_personality_v0'
C:\Users\Stephen\AppData\Local\Temp\ccc0a0w2.o:main.cpp:(.eh_frame$_ZNSt6vectorI8organismSaIS0_EED1Ev+0x13): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
"make": *** [build] Error 1
> Process Exit Code: 2
> Time Taken: 00:01
I can compile it if I comment out std::vector<organism> organisms; but I have no clue what's wrong with that line. It's exactly the same in my other project, which compiles fine.
You need to compile with g++.exe instead of gcc.exe so that it will know it needs to link with the C++ library.