Regarding compilation issue while linking with facebook folly libraries - c++

I have built the facebook folly libraries by using the following link given below
https://github.com/facebook/folly
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <typeinfo>
#include <vector>
#include "folly/FBVector.h"
int main()
{
folly::fbvector<int> vec;
folly::fbvector<int>::iterator vIter;
int i = 0;
for(i = 0;i < 10;i++)
{
vec.push_back(i);
}
for(i = 0 ; i <=vec.size();i++)
{
cout << vec[i] << endl;
}
vec.erase(vec.begin()+ 5);
vec.insert(vec.begin()+5,25);
vec.insert(vec.begin()+5,5);
vec.insert(vec.begin()+5,5);
sort(vec.begin(),vec.end());
cout << "Using Iteration Concept " << endl;
for(vIter = vec.begin() ; vIter != vec.end();vIter++)
{
cout << *vIter << endl;
}
return 0;
}
After writting this code ,just compiled it..when i was compiling,getting the following issue..
syscon#syscon-OptiPlex-3020:~/Documents/work/folly/folly-master$ g++ -o stl stl.cpp -std=c++11 -lboost_system
/tmp/cc2WeDlr.o: In function folly::usingJEMalloc()':stl.cpp:(.text._ZN5folly13usingJEMallocEv[_ZN5folly13usingJEMallocEv]+0x2d): undefined reference tofolly::usingJEMallocSlow()'
collect2: error: ld returned 1 exit status
which library i need to include inorder to solve this issue?

That particular symbol comes from: https://github.com/facebook/folly/blob/master/folly/Malloc.cpp so it's part of the compiled folly library (ie, it's not just a header-only dependency). You need to link against it.
Depending on how you compiled/installed folly will determine how to fix the problem, e.g. adding -lfolly to the compilation line.

Related

C++: Undefined Reference to 'CYourClass::Method()' [duplicate]

This question already has answers here:
How do I compile a .cpp file just to object file without calling linker
(2 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I am struggling to identify the issue with my code in my class inheritance. I've searched many places but can't quite seem to find the right answer. I've made some progress to fix many errors before this, but haven't wrapped it all up quite yet.
The main issue I am running into is undefined references to either methods or constructors in many instances throughout my files. I'm sure I'm probably just missing something stupidly simple somewhere, but being fairly new to C++, it's difficult for me to pinpoint exactly where the issue lies.
Below are the files to my code:
dog.h
#ifndef DOG_H
#define DOG_H
#include <string>
using namespace std;
class Dog{
public:
Dog();
~Dog(){};
string dogName = "";
string Breed = "";
int age = 0;
int weight = 0;
bool subjectToDiscount = false;
int riskWeight = 0;
float riskPremium = 0;
float basePremium = 0;
virtual float getPremium();
virtual Dog getDog(char b);
protected:
virtual float getBasePremium();
private:
};
#endif //DOG_H
dog.cpp
#include <iostream>
#include <string>
#include "dog.h"
#include "breeds.h"
using namespace std;
//---default constructor---//
Dog::Dog(){
};
//---methods---//
float Dog::getBasePremium(){
float val = 0;
//calculate base premium based on weight
return val;
};
float Dog::getPremium(){
float val = 0;
//calculate actual premium for the dog in question
if(this->riskWeight == 0){
val = this->basePremium;
}else if(this->weight > this-> riskWeight){
val = this->riskPremium;
}else val = this->basePremium;
//add discount if applicable
if(this->subjectToDiscount && this->age > 13) val *= 0.80;
//add 25% if over 50kg
if(this->weight > 50) val *= 1.25;
return val;
};
Dog Dog::getDog(char b){
bool recognized = false;
Dog *pup;
while(!recognized){
switch (b)
{
case 'p':
pup = new Pitbull();
recognized = true;
break;
case 'd':
pup = new Doberman();
recognized = true;
break;
case 'r':
pup = new Rottweiler();
recognized = true;
break;
default:
cout << "Breed code not recognized, please try again...\n";
break;
}
}
return *pup;
}
int main(){
return 0;
}
breeds.h
#ifndef BREEDS_H
#define BREEDS_H
#include "dog.h"
#include <string>
using namespace std;
class Pitbull : public Dog{
public:
Pitbull();
~Pitbull(){};
protected:
private:
};
class Doberman : public Dog{
public:
Doberman();
~Doberman(){};
protected:
private:
};
class Rottweiler : public Dog{
public:
Rottweiler();
~Rottweiler(){};
protected:
private:
};
#endif //BREEDS_H
breeds.cpp
#include <iostream>
#include <string>
#include "dog.h"
#include "breeds.h"
using namespace std;
//---constructors---//
Pitbull::Pitbull(){
this->Breed = "a Pitbull";
this->basePremium = 30.20f;
this->riskPremium = 35.15f;
this->riskWeight = 20;
this->subjectToDiscount = false;
}
Doberman::Doberman(){
this->Breed = "a Doberman";
this->basePremium = 28.16f;
this->riskPremium = 30.00f;
this->riskWeight = 35;
this->subjectToDiscount = true;
}
Rottweiler::Rottweiler(){
this->Breed = "a Rottweiler";
this->basePremium = 28.00f;
this->riskPremium = 29.75f;
this->riskWeight = 45;
this->subjectToDiscount = false;
}
int main(){
return 0;
}
main.cpp
#include <iostream>
#include <iomanip>
#include <string>
#include "dog.h"
#include "breeds.h"
using namespace std;
int getDogCount(){
int retVal = 0;
cout << "Please enter the number of dogs in your household: ";
cin >> retVal;
return retVal;
}
void run(){
cout << setiosflags(ios::fixed);
cout << setprecision(2);
int dogCount = 0;
float totalPremium = 0;
dogCount = getDogCount();
for(int i = 1; i <= dogCount; i++){
float premium = 0;
char breedCode = '.';
string dogName = "";
Dog pup;
cout << "Enter the name of dog #" << i << ": ";
cin.ignore();
getline(cin, dogName);
cout << "Enter the breed code for " << dogName << ": ";
cin >> breedCode;
cin.ignore();
pup = pup.getDog(breedCode);
pup.dogName = dogName;
cout << "Enter the current age for " << dogName << ": ";
cin >> pup.age;
cin.ignore();
cout << "Enter the current weight for " << dogName << ": ";
cin >> pup.weight;
premium = pup.getPremium();
cout << "\n";
}
}
int main(){
run();
return 0;
}
These are the errors I get after attempting to compile the 3 .cpp files:
dog.cpp
C:\Users\whitl\AppData\Local\Temp\ccFdz6zr.o: In function `Dog::getDog(char)':
C:\Sandbox\C++\Beginning\Wooffurs/dog.cpp:46: undefined reference to `Pitbull::Pitbull()'
C:\Sandbox\C++\Beginning\Wooffurs/dog.cpp:50: undefined reference to `Doberman::Doberman()'
C:\Sandbox\C++\Beginning\Wooffurs/dog.cpp:54: undefined reference to `Rottweiler::Rottweiler()'
collect2.exe: error: ld returned 1 exit status
breeds.cpp
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o: In function `Pitbull::Pitbull()':
C:\Sandbox\C++\Beginning\Wooffurs/breeds.cpp:10: undefined reference to `Dog::Dog()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o: In function `Doberman::Doberman()':
C:\Sandbox\C++\Beginning\Wooffurs/breeds.cpp:18: undefined reference to `Dog::Dog()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o: In function `Rottweiler::Rottweiler()':
C:\Sandbox\C++\Beginning\Wooffurs/breeds.cpp:26: undefined reference to `Dog::Dog()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV10Rottweiler[_ZTV10Rottweiler]+0x10): undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV10Rottweiler[_ZTV10Rottweiler]+0x18): undefined reference to `Dog::getDog(char)'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV10Rottweiler[_ZTV10Rottweiler]+0x20): undefined reference to `Dog::getBasePremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV8Doberman[_ZTV8Doberman]+0x10): undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV8Doberman[_ZTV8Doberman]+0x18): undefined reference to `Dog::getDog(char)'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV8Doberman[_ZTV8Doberman]+0x20): undefined reference to `Dog::getBasePremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV7Pitbull[_ZTV7Pitbull]+0x10): undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV7Pitbull[_ZTV7Pitbull]+0x18): undefined reference to `Dog::getDog(char)'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$_ZTV7Pitbull[_ZTV7Pitbull]+0x20): undefined reference to `Dog::getBasePremium()'
C:\Users\whitl\AppData\Local\Temp\ccbAKpbb.o:breeds.cpp:(.rdata$.refptr._ZTV3Dog[.refptr._ZTV3Dog]+0x0): undefined reference to `vtable for Dog'
collect2.exe: error: ld returned 1 exit status
main.cpp
C:\Users\whitl\AppData\Local\Temp\ccgKyBTC.o: In function `run()':
C:\Sandbox\C++\Beginning\Wooffurs/main.cpp:30: undefined reference to `Dog::Dog()'
C:\Sandbox\C++\Beginning\Wooffurs/main.cpp:40: undefined reference to `Dog::getDog(char)'
C:\Sandbox\C++\Beginning\Wooffurs/main.cpp:50: undefined reference to `Dog::getPremium()'
C:\Users\whitl\AppData\Local\Temp\ccgKyBTC.o:main.cpp:(.rdata$.refptr._ZTV3Dog[.refptr._ZTV3Dog]+0x0): undefined reference to `vtable for Dog'
collect2.exe: error: ld returned 1 exit status
Thank you to anyone that helps, I really appreciate it. Please give my any pointers on my code as well, and how to possibly better pose questions on Stack Overflow.
It looks as though you are doing something like:
g++ dog.cpp -o dog
Without the -c flag (for compile only, don't link) the compiler will attempt to make an executable from that one file.
Generally speaking you need to do one of the following:
Compile each cpp file separately, and then link at the end
# compilation
g++ -c dog.cpp -o dog.o
g++ -c breeds.cpp -o breeds.o
g++ -c main.cpp -o main.o
# now link the 3 object files into the exe
g++ -o myApp main.o dog.o breeds.o
Compile them in one go
g++ -o myApp main.cpp dog.cpp breeds.cpp
Use a makefile
all : myApp
# dog.o depends on dog.cpp & breeds.h. When those change, run line below
dog.o: dog.cpp breeds.h
gcc -c -o dog.o dog.cpp
breeds.o: breeds.cpp dog.h
gcc -c -o breeds.o breeds.cpp
main.o: main.cpp breeds.h dog.h
gcc -c -o main.o dog.cpp
# final app depends on the object files, when they change, recompile.
myApp: main.o dog.o breeds.o
gcc -o myApp main.o dog.o breeds.o
clean:
rm -f *.o
Use some IDE to manage this for you (or use something like cmake)

CLN not recognized by compiler

I have installed CLN from cygwin and it has an examples folder. I copied an example and tried to compile it and it wont let me
#include <cln/real.h>
#include <cln/integer.h>
#include <cln/io.h>
#include <cln/integer_io.h>
#include <cln/numtheory.h>
using namespace std;
using namespace cln;
int main() {
int n;
cin >> n;
cl_R x = (cl_R) n;
cl_I p = nextprobprime(x);
cout << p << "\n";
return 0;
}
It gives an error
fatal error: cln/real.h: No such file or directory
1 | #include <cln/real.h>
| ^~~~~~~~~~~~
compilation terminated.
My plan was to install ginac so I had to install cln first. Do I need to add a path variable or something to allow it to compile? If so how do I do that
Also is it possible to write
#include <cln>
or something like this so that all headers are included instead of doing it one by one. This is my first external library. I plan to use more

C++11 Undefined reference to function

I unable to find a solution to my problem, I think it has something to do with overloading functions but I can't seem to figure out how to resolve it.
here is my function.cpp
#include "CountLetter.h"
int Countletter(string sentence, char letter) {
int size = sentence.length();
int toReturn = 0;
for (int i = 0; i<= size; ++i) {
if (sentence[i] == letter) {
toReturn++;
}
}
return toReturn;
}
Here is my function.h
#ifndef FN_H
#define FN_H
#include <iostream>
using namespace std;
int CountLetter(string sentence, char letter);
#endif
My main.cpp
#include "CountLetter.h"
int main() {
string sent = "";
char let = ' ';
int times = 0;
cout << "Enter a sentence.\n";
getline(cin, sent);
cout << "Enter a letter.\n";
cin >> let;
times = CountLetter(sent, let);
cout << "The letter " << let << " occurred " << times << " time(s).\n";
return 0;
}
and finally my makefile
lab16: lab16.o CountLetter.o
g++ -std=c++11 -o lab16 lab16.o CountLetter.o
lab16.o: lab16.cpp
g++ -std=c++11 -o lab16.o -c lab16.cpp
CountLetter.o: CountLetter.h CountLetter.cpp
g++ -std=c++11 -o CountLetter.o -c CountLetter.cpp
and my errors
lab16.o: In function `main':
lab16.cpp:(.text+0xb4): undefined reference to
`CountLetter(std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, char)'
collect2: error: ld returned 1 exit status
Makefile:2: recipe for target 'lab16' failed
make: *** [lab16] Error 1
Thanks!
C and C++ are case sensitive:
Definition:
int Countletter(string sentence, char letter) {
Usage:
times = CountLetter(sent, let);
When facing with a linker error, suspect misspelling as one of the scenarios.
Did you get their subtle difference?
Countletter
CountLetter
Your CountLetter function is in the function.h so
change your #include "CountLetter.h" into #include "function.h"

Combination of CImg and fftw++ fails

I want to do a fft in my c++ project, and show it afterwards as an image. In order to do the fft, I am using fftw++, and for displaying the image I wanted to use the CImg-library. Thus I started with the demo project from here. When compiling it, everything works. As soon as I add the CImg-header, it fails with the error
test.cpp: In function ‘int main()’:
test.cpp:18:12: error: ‘f’ was not declared in this scope
Complex *f=ComplexAlign(n);
My file looks like
#include "fftw++.h"
#include "CImg.h"
// Compile with:
// g++ -I .. -fopenmp example0.cc ../fftw++.cc -lfftw3 -lfftw3_omp
//using namespace std;
//using namespace utils;
//using namespace fftwpp;
//using namespace cimg_library;
int main()
{
fftwpp::fftw::maxthreads=get_max_threads();
std::cout << "1D complex to complex in-place FFT, not using the Array class"
<< std::endl;
unsigned int n=4;
Complex *f=utils::ComplexAlign(n);
fftwpp::fft1d Forward(n,-1);
fftwpp::fft1d Backward(n,1);
for(unsigned int i=0; i < n; i++) f[i]=i;
std::cout << "\ninput:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
Forward.fft(f);
std::cout << "\noutput:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
Backward.fftNormalized(f);
std::cout << "\ntransformed back:" << std::endl;
for(unsigned int i=0; i < n; i++) std::cout << f[i] << std::endl;
utils::deleteAlign(f);
}
and is compiled with
g++ -I .. -fopenmp test.cpp ../fftw++.cc -lfftw3 -lfftw3_omp
My g++ version is 4.8.5. Adding the Complex.h-header does not help either. What can I do in order to combine both libraries?
Edit: Further research shows that using the C-library complex.h and CImg.h results in a lot of compilation problems, combining the library Complex.h from the fftw++-package results also in errors, only the complex-include from C++ can be used together with the CImg.h-include file. Reason: Unknown till now.
My solution (even if it is not perfect) is, that I created a second cpp-file:
second.cpp:
#include "CImg.h"
//Code for images
void example(void){
}
and an include-file for that:
second.h:
#ifndef SECOND_H
#define SECOND_H
void example(void);
#endif /* SECOND_H */
If I only include that include file instead of CImg.h, I can use both fftw++ and CImg.

Error linking clang++ with dlib and intel mkl

Here is a small program that incorporates a call to an Intel MKL library function and DLIB's optimization routine find_min_using_approximate_derivatives.
The code runs perfectly when compiled with the intel C++ compiler: icpc using the invocation:
icpc main.cpp -I /Users/Username/Code/dlib-18.16 -DDLIB_USE_BLAS -I$MKLROOT/include -L$MKLROOT/lib/ -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_lp64 -DENABLE_DLIB -DDLIB_USE_BLAS
or by disabling the DLIB related portion of the code via:
clang++ main.cpp -I$MKLROOT/include -L$MKLROOT/lib/ -lmkl_core -lmkl_intel_thread -lpthread -lm -lmkl_intel_lp64
C++ code
// #define ENABLE_DLIB
#ifdef ENABLE_DLIB
#include "dlib/optimization.h"
#endif
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "mkl.h"
using namespace std;
#ifdef ENABLE_DLIB
using namespace dlib;
#endif
template<typename T>
void printArray(T *data, string name, int len){
cout << name << "\n";
for(int i=0;i<len;i++){
cout << data[i] << " ";
}
cout << "\n";
}
#ifdef ENABLE_DLIB
typedef matrix<double,0,1> column_vector;
double rosen (const column_vector& m)
{
const double x = m(0);
const double y = m(1);
return 100.0*pow(y - x*x,2) + pow(1 - x,2);
}
#endif
int main()
{
#ifdef ENABLE_DLIB
column_vector starting_point(2);
starting_point = 4, 8;
find_min_using_approximate_derivatives(bfgs_search_strategy(),
objective_delta_stop_strategy(1e-7),
rosen, starting_point, -1);
cout << "\nBFGS rosen minimum lies at:\n";
cout << "x = " << starting_point(0) << endl;
cout << "y = " << starting_point(1) << endl;
#endif
int len=10;
double *x=new double[len];
double *y=new double[len];
for(int i=0;i<len;i++){
x[i]=(double)std::rand()/RAND_MAX;
y[i]=(double)std::rand()/RAND_MAX;
}
printArray<double>(x, "x", len);
printArray<double>(y, "y", len);
//sum(x)
double x_sum=cblas_dasum(len,x,1);
cout<< "sum(x): "<< x_sum <<"\n";
}
Nevertheless, it fails replacing icpc with clang++ above with multiple errors of the following kind:
Error
In file included from /opt/intel/composer_xe_2015.3.187/mkl/include/mkl.h:48:
/opt/intel/composer_xe_2015.3.187/mkl/include/mkl_cblas.h:584:6: error: conflicting types for 'cblas_cgemm'
void cblas_cgemm(const CBLAS_LAYOUT Layout, const CBLAS_TRANSPOSE TransA,
^
/Users/Username/Code/dlib-18.16/dlib/matrix/matrix_blas_bindings.h:75:18: note: previous declaration is here
void cblas_cgemm(const enum CBLAS_ORDER Order, const enum CBLAS_TRANSPOSE TransA,
...suggesting a conflict between the cblas_* routines mirrored in MKL. The documentation for DLIB suggests using the DLIB_USE_BLAS preprocessor directive in order for it to link with MKL but evidently it doesn't seem to help using clang++.
How do I make this work?