Nasty error when initializing vector - c++

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.

Related

How to print current timestamp in C++

I currently have this code:
#include <iostream>
#include <time.h>
int main() {
printf("%i", time(0));
return 0;
}
Compile with gcc:
gcc source.cpp -o program
Error:
/tmp/ccTUJX4u.o: In function `__static_initialization_and_destruction_0(int, int)':
source.cpp:(.text+0x1a9): undefined reference to `std::ios_base::Init::Init()'
source.cpp:(.text+0x1b8): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status
I just want to print the current timestamp I tried stuff with the chrono and ctime modules but nothing works is it really that hard to print the current time in C++?
Please help

C++ undefined reference to `__atomic_load_16'

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

Fixing undefined reference to dlopen() and dlcose()

I have created a simple C++ application. I can compile it, and it works fine. But now I need to load the library dynamically, and I have added dlfnc.h to my project and added some more code:
#include <iostream>
#include <dlfcn.h>
void *mylib;
int eret;
using namespace std;
int main() {
mylib = dlopen("mylib.so", RTLD_LOCAL | RTLD_LAZY);
eret = dlclose(mylib);
cout << "!!!Hello, World!!!" << endl; // Prints !!!Hello, World!!!
return 0;
}
Compiling:
cd ~/workspace/LinuxGcc/src
g++ LinuxGcc.cpp
And I got a compilation error:
/tmp/ccxTLiGY.o: In function `main':
LinuxGcc.cpp:(.text+0xf): undefined reference to `dlopen'
LinuxGcc.cpp:(.text+0x25): undefined reference to `dlclose'
collect2: error: ld returned 1 exit status
dlfcn.h exist in /usr/include/.
Where is the problem?
From dlopen(3):
Link with -ldl.
so
g++ LinuxGcc.cpp -ldl
will be OK.
The solution is very simple. Add the -ldl flag for linking.
In case of the Bazel build system, linkopts = ['-ldl'].

Geany simple linking

When I run and build a simple program, it fails.
Here is the error message:
g++ -Wall -o "main" "main.cpp" (in directory: /home/markuz/Desktop)
/tmp/ccHV9wPu.o: In function main':
main.cpp:(.text+0x11): undefined reference toTest::display()'
collect2: ld returned 1 exit status
Compilation failed.
Here are the files. The compile and build command is the default of geany 1.22
//main.cpp
#include "imba.h"
int main(){
Test t;
t.display();
return 0;
}
//imba.h
class Test{
public:
void display();
};
//imba.cpp
#include <iostream>
#include "imba.h"
void Test::display(){
std::cout << "oi";
}
Any ideas about this?
Thanks.
You need to also add the imba.cpp file in the compilation step. Although you have included the header in your main file, you have not compiled the source for it and so the linker cannot find the object file for imba.cpp - that is what the error is complaining about

Errors in compiling boost:thread in Windows Eclipse C++ plugin

I can get other boost libraries to work in Eclipse C++ plugin, but have problem in boost thread. What's the problem?
#include <boost/thread/thread.hpp>
#include <iostream>
using namespace std;
void hello()
{
cout<<"hello world!"<<endl;
}
int main()
{
boost::thread thrd(&hello);
cout<<"Just a test!"<<endl;
return 0;
}
Error messages:
** Rebuild of configuration Debug for project Hello **
Internal Builder is used for build **
g++ -IC:\boost_1_47_0 -O0 -g3 -Wall -c -fmessage-length=0 -osrc\hello.o ..\src\hello.cpp
In file included from C:\boost_1_47_0/boost/thread/win32/thread_data.hpp:12,
from C:\boost_1_47_0/boost/thread/thread.hpp:15,
from ..\src\hello.cpp:1:
C:\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:59: warning: inline function 'void* boost::detail::allocate_raw_heap_memory(unsigned int)' declared as dllimport: attribute ignored
C:\boost_1_47_0/boost/thread/win32/thread_heap_alloc.hpp:69: warning: inline function 'void boost::detail::free_raw_heap_memory(void*)' declared as dllimport: attribute ignored
g++ -LC:\boost_1_47_0\stage\lib -oHello.exe src\hello.o -lboost_filesystem-mgw46-mt-d-1_47 -lboost_regex-mgw46-mt-1_47 -lboost_system-mgw46-mt-1_47 -lboost_thread-mgw46-mt-1_47 -lboost_date_time-mgw46-mt-1_47
src\hello.o: In function `main':
C:\Users\cmin\workspace\Hello\Debug/../src/hello.cpp:15: undefined reference to `_imp___ZN5boost6threadD1Ev'
C:\Users\cmin\workspace\Hello\Debug/../src/hello.cpp:15: undefined reference to `_imp___ZN5boost6threadD1Ev'
src\hello.o: In function `thread<void (*)()>':
C:/boost_1_47_0/boost/thread/detail/thread.hpp:204: undefined reference to `_imp___ZN5boost6thread12start_threadEv'
collect2: ld returned 1 exit status
Build error occurred, build is stopped
Time consumed: 1044 ms.