g++ compilation error - c++

So i've got this very basic OOP example and i want to compile it in Xubuntu but I get errors
the CThermo.h file
class CThermo
{
public:
void SetTemp(int newTemp);
int ReturnTemp();
void ChangeTemp(int deltaTemp);
private:
int m_temp;
};
the CThermo.cpp file
#incude "CThermo.h"
void CThermo::SetTemp(int newTemp)
{
m_temp = newTemp;
}
int CThermo::ReturnTemp()
{
return m_temp;
}
void CThermo::ChangeTemp(int deltaTemp)
{
m_temp += deltaTemp;
}
the main.cpp file
#include "CThermo.h"
#include <iostream>
using std::cout;
int main()
{
CThermo roomTemp;
roomTemp.SetTemp(20);
cout << "the temp is : "<< roomTemp.ReturnTemp() << "\n";
roomTemp.ChangeTemp(5);
cout << "after changing the temp, the room temp is : " << roomTemp.ReturnTemp();
cout << "test";
return 0;
}
the command to compile is "g++ main.cpp -o Main"
and this are the errors I get
/tmp/ccXajxEY.o: In function `main':
main.cpp:(.text+0x1a): undefined reference to `CThermo::SetTemp(int)'
main.cpp:(.text+0x26): undefined reference to `CThermo::ReturnTemp()'
main.cpp:(.text+0x6c): undefined reference to `CThermo::ChangeTemp(int)'
main.cpp:(.text+0x78): undefined reference to `CThermo::ReturnTemp()'
collect2: error: ld returned 1 exit status

You have to compile both main.cpp and CThermo.cpp using:
g++ CThermo.cpp main.cpp -o Main

Related

Linker error with simple threaded program (symbols from boost_chrono missing)

I'm learning boost::timed_mutex
The follwing code cannot be compiled:
#include <boost/thread.hpp>
#include <boost/chrono.hpp>
#include <iostream>
void wait(int seconds)
{
boost::this_thread::sleep_for(boost::chrono::seconds{seconds});
}
boost::timed_mutex mutex;
void thread1()
{
using boost::this_thread::get_id;
for (int i = 0; i < 5; ++i)
{
wait(1);
boost::unique_lock<boost::timed_mutex> lock{mutex};
std::cout << "Thread " << get_id() << ": " << i << std::endl;
boost::timed_mutex *m = lock.release();
m->unlock();
}
}
void thread2()
{
using boost::this_thread::get_id;
for (int i = 0; i < 5; ++i)
{
wait(1);
boost::unique_lock<boost::timed_mutex> lock{mutex,
boost::try_to_lock};
if (lock.owns_lock() || lock.try_lock_for(boost::chrono::seconds{1}))
{
std::cout << "Thread " << get_id() << ": " << i << std::endl;
}
}
}
int main()
{
boost::thread t1{thread1};
boost::thread t2{thread2};
t1.join();
t2.join();
}
My compile command is:
g++ -std=c++11 unique_lock.cpp -o unique_lock -g -lboost_system -lboost_thread-mt -pthread -lboost_timer
The error is something like the following:
/bin/ld: /tmp/ccRVKHNh.o: undefined reference to symbol '_ZN5boost6chrono12system_clock3nowEv'
/usr/lib64/libboost_chrono.so.1.53.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
What's wrong?
Apparently -lboost_chrono is missing.
Demo

Compilation issue while using Folly Futures

#include <folly/futures/Future.h>
#include<iostream>
using namespace folly;
using namespace std;
void foo(int x)
{
// do something with x
cout << "foo(" << x << ")" << endl;
}
// ...
int main()
{
cout << "making Promise" << endl;
Promise<int> p;
Future<int> f = p.getFuture();
return 0;
}
LD_LIBRARY_PATH is set to /usr/local/lib
When I compile, using
g++ -std=c++11 sample.cpp -pthread -llzma -lz -lsnappy -llz4 -liberty \
-ljemalloc -levent -ldouble-conversion -lssl -lgflags -lglog -lboost_system
I get this error message:
/tmp/ccuDCM19.o: In function `main':
sample.cpp:(.text+0x1a7): undefined reference to `folly::Future<int>::~Future()'
/tmp/ccuDCM19.o: In function `folly::RequestContext::setContext(std::shared_ptr<folly::RequestContext>)':
sample.cpp:(.text._ZN5folly14RequestContext10setContextESt10shared_ptrIS0_E[_ZN5folly14RequestContext10setContextESt10shared_ptrIS0_E]+0x11): undefined reference to `folly::RequestContext::getStaticContext()'
/tmp/ccuDCM19.o: In function `folly::Promise<int>::getFuture()':
sample.cpp:(.text._ZN5folly7PromiseIiE9getFutureEv[_ZN5folly7PromiseIiE9getFutureEv]+0x36): undefined reference to `folly::Future<int>::Future(folly::detail::Core<int>*)'
collect2: error: ld returned 1 exit status
Issue got resolved by adding -lfolly.

C++ error: undefined reference to function

I have three files:
Two .cpp files (Reader.cpp and algo.cpp) and one header file (algo.h).
In Reader.cpp file, its function PacketHandler calls a function present in algo.cpp.
Reader.cpp
void PacketHandler(Packet* sniff_packet, void* user) {
std::string payload;
RawLayer* raw_payload = sniff_packet->GetLayer<RawLayer>();
if (raw_payload) {
/* Summarize some data */
cout << "[+] ------- [+]" << endl;
TCP* tcp_layer = sniff_packet->GetLayer<TCP>();
cout << "[#] TCP packet from source port: " << dec << tcp_layer->GetSrcPort() << endl;
cout << "[#] With Payload: " << endl;
payload = raw_payload->GetStringPayload();
SPPM(payload); // Function present in algo.cpp
}
}
void main() { // }
algo.h
#ifndef algo_H_
#define algo_H_
#include <string>
using namespace std;
void SPPM (std::string input);
#endif
algo.cpp
#include "algo.h"
#define byte uint8_t
using namespace std;
void SPPM(std::string input){ //definition }
When I compile the code using g++, I get this error:
In function `PacketHandler(Crafter::Packet*, void*)':
/home/maleeha/libcrafter_latest/libcrafter-master/libcrafter/pcap_reader.cpp:32: undefined reference to `SPPM(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'collect2: ld returned 1 exit status
I am executing my code this way:
g++ -c Reader.cpp -o Reader.o -g -lcrafter
g++ -c algo.cpp -o algo.o
g++ -o program Reader.o algo.o -lcrafter
./program Packets.pcap
Why is this error coming?

undefined reference to `__gcov_flush'

I am trying same,
http://www.linuxforums.org/forum/suse-linux/135465-gcov-g.html
Code from the link,
#include <iostream>
using namespace std;
void one(void);
void two(void);
void __gcov_flush(void);
int main(void)
{
int i;
while(true)
{
__gcov_flush();
cout << "Enter a number(1-2), 0 to exit " << endl;
cin >> i;
if ( i == 1 )
one();
else if ( i == 2 )
two();
else if ( i == 0 )
break;
else
continue;
}
return 0;
}
void one(void)
{ cout << "One is called" << endl; }
void two(void)
{ cout << "Two is called" << endl; }
but for me also it gives,
test.cpp:(.text+0x1d9): undefined reference to `__gcov_flush()'
collect2: ld returned 1 exit status
Tried the followings,
g++ -fprofile-arcs test.cpp
g++ -fprofile-arcs -g test.cpp
g++ -fprofile-arcs -ftest-coverage -g test.cpp
g++ -fprofile-arcs -ftest-coverage -g test.cpp -lgcov
I have also tried the "-lgcov" & "extern void __gcov_flush(void)" as mentioned in link above. I am currently on Ubuntu12.04 and g++ 4.6
So, I want to know if there is solution for this or gcov_flush doesnt work anymore.
void __gcov_flush();
Since the code is compiled as C++, this declares the existence of a C++ function of that name. C++ functions are subject to name mangling, so the (C++) symbol is not found in the (C) link library, and the linker (rightfully) complains about it.
If you declare the function, declare it as a function with C linkage:
extern "C" void __gcov_flush();
This should do the trick.
Note the commend by Paweł Bylica -- __gcov_flush() has been removed in GCC 11, you should use __gcov_dump().
I fixed this issue changing the settings.
Test Project --> Build Settings
Instrument Program Flow = Yes

Linker error compiling keyczar program

I am using g++ -lkeyczar -lcrypto -o basic_encrypt -Wall -O2 base_encrypt.cpp to compile the following code:
#include <cassert>
#include <iostream>
#include <string>
#include <keyczar/keyczar.h>
void EncryptAndDecrypt(const std::string& location) {
keyczar::Keyczar* crypter = keyczar::Crypter::Read(location);
if (!crypter)
return;
std::string input = "Secret message";
std::string ciphertext;
std::cout << "Plaintext: " << input << std::endl;
bool result = crypter->Encrypt(input, &ciphertext);
if (result) {
std::cout << "Ciphertext (Base64w): " << ciphertext << std::endl;
std::string decrypted_input;
bool result = crypter->Decrypt(ciphertext, &decrypted_input);
if (result)
assert(input == decrypted_input);
}
delete crypter;
}
int main(int argc, char** argv) {
if (argc != 2) {
std::cout << "An absolute key set location must be provided as argument"
<< std::endl;
return 1; // error
}
// The first argument must represent the keyset's location
const std::string location(argv[1]);
EncryptAndDecrypt(location);
return 0;
}
Which is a tutorial taken from here
However, I am running into the following error:
/tmp/ccNlack3.o: In function `EncryptAndDecrypt(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)':
base_encrypt.cpp:(.text+0xf): undefined reference to `keyczar::Crypter::Read(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
collect2: ld returned 1 exit status
I am unable to solve this since I know that I am already giving the library flags while compiling. Why is it still unable to link correctly ?
Put the library flags at the end of the command line:
g++ -o basic_encrypt -Wall -O2 base_encrypt.cpp -lkeyczar -lcrypto