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.
Related
I am trying to compile a c++ project in Eclipse CDT with third party C project (minimodem https://github.com/kamalmostafa/minimodem), Currently my project simply contains a few cpp files and folder inside the source folder with C files. I have included c headers the main.cpp c files using
extern "C"
{
#include "minimodem/simpleaudio.h"
#include "minimodem/fsk.h"
#include "minimodem/databits.h"
}
the code compiles but breaks at linking with below error
**** Build of configuration Debug for project Sample_Test ****
make all
Building target: Sample_Test
Invoking: GCC C++ Linker
g++ -L/usr/include -o "Sample_Test" ./minimodem/baudot.o ./minimodem/databits_ascii.o ./minimodem/databits_baudot.o ./minimodem/databits_binary.o ./minimodem/databits_callerid.o ./minimodem/databits_uic.o ./minimodem/fsk.o ./minimodem/simple-tone-generator.o ./minimodem/simpleaudio-alsa.o ./minimodem/simpleaudio-benchmark.o ./minimodem/simpleaudio-pulse.o ./minimodem/simpleaudio-sndfile.o ./minimodem/simpleaudio.o ./minimodem/uic_codes.o ./aes256.o ./main.o -lfftw3f -lasound -lpulse-simple -lpulse -lsndfile
./minimodem/simpleaudio.o: In function `simpleaudio_open_stream':
/home/user1/workspace/Sample_Test/Debug/../minimodem/simpleaudio.c:88: undefined reference to `simpleaudio_backend_pulseaudio'
makefile:45: recipe for target 'Sample_Test' failed
/home/user1/workspace/Sample_Test/Debug/../minimodem/simpleaudio.c:99: undefined reference to `simpleaudio_backend_alsa'
/home/user1/workspace/Sample_Test/Debug/../minimodem/simpleaudio.c:105: undefined reference to `simpleaudio_backend_pulseaudio'
collect2: error: ld returned 1 exit status
make: *** [Sample_Test] Error 1
**** Build Finished ****
inside simpleaudio.c the function simpleaudio_open_stream (third party C code)
..
#include "simpleaudio.h"
#include "simpleaudio_internal.h"
..
simpleaudio_open_stream(
...
simpleaudio *sa = calloc(1, sizeof(simpleaudio));
...
#if USE_PULSEAUDIO
sa->backend = &simpleaudio_backend_pulseaudio;
#elif USE_ALSA
..
#if USE_ALSA
case SA_BACKEND_ALSA:
sa->backend = &simpleaudio_backend_alsa;
break;
#endif
#if USE_PULSEAUDIO
case SA_BACKEND_PULSEAUDIO:
sa->backend = &simpleaudio_backend_pulseaudio;
break;
#endif
...
)
the structs linker is unable to find reside in simpleaudio_internal.h
extern const struct simpleaudio_backend simpleaudio_backend_benchmark;
extern const struct simpleaudio_backend simpleaudio_backend_sndfile;
extern const struct simpleaudio_backend simpleaudio_backend_alsa;
extern const struct simpleaudio_backend simpleaudio_backend_pulseaudio;
I moved from Windows to Ubuntu and I wanted to try some C++ programming on Ubuntu. So here is very simple code and very stupid error which I can't resolve:
horse.h
#ifndef _horse_
#define _horse_
class Horse{
int speed;
public:
void saySomething();
};
#endif
horse.cpp
#include "horse.h"
#include <iostream>
using namespace std;
void Horse::saySomething(){
cout << "iiiihaaaaaaa brrrrr."<<endl;
}
and Main.cpp
#include "horse.h"
int main(){
Horse h;
h.saySomething();
}
After I compile (compilation is successful) and run this I get this error message:
/tmp/ccxuDyrd.o: In function `main':
Main.cpp:(.text+0x11): undefined reference to `Horse::saySomething()'
collect2: ld returned 1 exit status
Please help me somehow.
Try
g++ -c main.cpp horse.cpp (to compile)
g++ -o a.out main.o horse.o (to link)
It seems you only compiled your code but did not link the resulting object files. You probably invoked the compiler like this:
g++ main.cpp
You should instead compile every *.cpp file separately and then link each resulting *.o file. And you should do this with a Makefile.
Actually, the basic idea is the same on Windows with MSVC. The compiler produces object files, the linker links them together.
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
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.
I was looking for a lock-free hash table implementation in C or C++ and found one: SunriseDD. That was the good news :) For a few days I am trying to get this up and running but with no luck. I can compile downloaded sources as a static library using GCC C compiler. And I get libSunriseDD.a archive.
So far so good :D
I have created a simple C++ application to wrap the SunriseDD C implementation of the hash table. Here is the main.cpp:
#include <iostream>
#include <dd_data_dictionary.h>
using namespace std;
struct Node{
int oid;
int x, y;
};
struct HashTableEntry{
Node *node;
int idx;
};
void copyFunction(void * source, void * target){
target = source;
}
template<typename T>class HashTableDD{
private:
dd_dictionary objects;
public:
HashTableDD() {
objects = dd_new_dictionary(); // creating dictionary
dd_set_object_copy_function_for_dictionary(objects, copyFunction); // setting copy function
}
~HashTableDD() {
dd_dispose_dictionary(objects);
}
bool insert(T obj, long key){
return dd_add_object_for_key(objects, (char *)key, (void*)obj);
}
bool remove(long key){
return dd_remove_object_for_key(objects, (char *)key);
}
T find(long key) {
return (T)dd_object_for_key(objects, (char *)key, false);
}
};
int main(int argc, char ** argv){
HashTableDD<HashTableEntry*> *ht = new HashTableDD<HashTableEntry*>();
HashTableEntry* hte = new HashTableEntry;
hte->idx = 1;
hte->node = NULL;
ht->insert(hte, 1);
HashTableEntry* hte2 = new HashTableEntry;
hte2->idx = 2;
Node n;
n.oid = 10; n.x = 10; n.y = 10;
hte2->node = &n;
ht->insert(hte2, 2);
HashTableEntry* ret = ht->find(1);
if(ret != NULL){
cout << "hte. idx: " << ret->idx << " node: " << ret->node << endl;
}
ht->remove(1);
ht->remove(2);
delete hte; delete hte2; delete ht;
return 0;
}
But the linker is not happy about that:
:~/Desktop/HashTable$ make
Building file: main.cpp
Invoking: GCC C++ Compiler
g++ -ISunriseDD/build/../ -O0 -g3 -m64 -c -o"build/main.o" "main.cpp"
Finished building: main.cpp
Building target: build/HashTableDD
Invoking: GCC C++ Linker
g++ -LSunriseDD/build/ -lSunriseDD -o build/HashTableDD build/main.o
build/main.o: In function `HashTableDD':
/home/robertas/Desktop/HashTable/main.cpp:32: undefined reference to `dd_new_dictionary()'
/home/robertas/Desktop/HashTable/main.cpp:33: undefined reference to `dd_set_object_copy_function_for_dictionary(void*, void (*)(void*, void*))'
build/main.o: In function `HashTableDD<HashTableEntry*>::insert(HashTableEntry*, long)':
/home/robertas/Desktop/HashTable/main.cpp:40: undefined reference to `dd_add_object_for_key(void*, char const*, void*)'
build/main.o: In function `HashTableDD<HashTableEntry*>::find(long)':
/home/robertas/Desktop/HashTable/main.cpp:47: undefined reference to `dd_object_for_key(void*, char const*, bool)'
build/main.o: In function `HashTableDD<HashTableEntry*>::remove(long)':
/home/robertas/Desktop/HashTable/main.cpp:44: undefined reference to `dd_remove_object_for_key(void*, char const*)'
build/main.o: In function `~HashTableDD':
/home/robertas/Desktop/HashTable/main.cpp:36: undefined reference to `dd_dispose_dictionary(void*)'
collect2: ld returned 1 exit status
make: *** [build/HashTableDD] Error 1
Any ideas what am I doing wrong here? Do I link the SunriseDD library incorrectly?
BTW I have the following directory listing where my main.cpp resides:
+HashTable
|--+build
| |--main.o
|---main.cpp
|--+SunriseDD
|--+build
| |--libSunriseDD.a
| |--other object files
|--headers and source files of SunriseDD
Thanks for the help!
Put -lSunriseDD last on the linker line. The linker processes arguments left to right, and searches libraries for currently undefined symbols when it processes a static library.
Also, unless the library is C++-aware, wrap includes in extern "C".
extern "C" {
#include <dd_data_dictionary.h>
}