troubles with compiling a c++ file including exprTk - c++

I have some troubles compiling a c++ code including exprtk. I want to compile an given example of the package (I called it parser.cpp):
#include <cstdio>
#include <string>
#include "exprtk.hpp"
template <typename T>
void trig_function()
{
typedef exprtk::symbol_table<T> symbol_table_t;
typedef exprtk::expression<T> expression_t;
typedef exprtk::parser<T> parser_t;
std::string expression_string = "clamp(-1.0,sin(2 * pi * x) + cos(x / 2 * pi),+1.0)";
T x;
symbol_table_t symbol_table;
symbol_table.add_variable("x",x);
symbol_table.add_constants();
expression_t expression;
expression.register_symbol_table(symbol_table);
parser_t parser;
parser.compile(expression_string,expression);
for (x = T(-5); x <= T(+5); x += T(0.001))
{
T y = expression.value();
printf("%19.15f\t%19.15f\n",x,y);
}
}
int main()
{
trig_function<double>();
return 0;
}
Therefore I use the following commands in cmd:
g++ -c -o parser.o -Wa,-mbig-obj -I include parser.cpp
g++ -o parser.exe -s parser.o
The exprtk.hpp file is in an include folder in the same directiory as the parser.cpp file.
The first problem is, that the parser.o file is very large (~ 32 Mb) and creating the .exe file take such a long time that I abort the compilation. Furthermore without the -Wa,-mbig-obj flag I get an error. Also I think there is no linking needed because all the code is included in the .hpp file and there are no .dll files or something else. By dropping the flag the error is:
C:/Rtools/mingw_64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.3/../../../../x86_64-w64-mingw32/bin/as.exe:
parser.o: too many sections (88691)
C:\..\AppData\Local\Temp\ccE7ythI.s: Assembler messages:
C:\..\AppData\Local\Temp\ccE7ythI.s: Fatal error: can't write parser.o: File too big
C:/Rtools/mingw_64/bin/../lib/gcc/x86_64-w64-mingw32/4.9.3/../../../../x86_64-w64-mingw32/bin/as.exe: parser.o:
too many sections (88691)
C:\..\AppData\Local\Temp\ccE7ythI.s: Fatal error:
can't close parser.o: File too big
The source is given via GitHub here. The same error as above occurs if I run make in cmd in the folder where the Makefile is.
Do I miss something or am I too foolish importing the exprtk.hpp file correctly? Any suggestions?

You have to have the -mbig-obj flag, which allows for big object files.
As explained here, .obj files have 65536 sections by default.
Exprtk is one big template heavy library (hpp is 1.4M) so it will take a long time to compile and need lots of sections in the object file.

Related

Compilation steps for a c++ file with Rcpp header file

I am trying this code on gedit and compiling by g++ compiler on terminal.
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
double sumC(NumericVector x) {
int n = x.size();
double total = 0;
for(int i = 0; i < n; ++i) {
total += x[i];
}
return total;
}
// [[Rcpp::export]]
double meanC(NumericVector x) {
return sumC(x) / x.size();
}
Error occurred for the header file.
fatal error: Rcpp.h: No such file or directory
I have compiled like this: g++ -I /usr/ r1.cpp -o c0 -L /usr/ -lRcpp
Also i tried :g++ -I /usr/lib/R/site-library/Rcpp/include/ r1.cpp -o c0 -L /usr/lib/R/site-library/Rcpp/libs/ -lRcpp . THen got error like fatal
error: R.h: No such file or directory #include <R.h>
Locations:
locate Rcpp.h:/usr/lib/R/site-library/Rcpp/include/Rcpp.h
locate R.h:/usr/share/R/include/R.h
I have tried with make file also.
My make file:
all:
g++ rcpp.cpp -o obj
compile:
I have attached all the depending header files in a single folder. Still getting the errors for Rcpp.
Any one knows how to compile this through terminal?
You can compile this file with
g++ -I/usr/share/R/include -I/usr/lib/R/site-library/Rcpp/include -c rcpp.cpp -o rcpp.o
However, I do not understand why you want to do this. In order to make such C++ functions callable from R, several additional steps are necessary:
C++ wrapper functions that translate to an interface based on R's SEXP.
R wrapper functions that call the C++ wrapper functions via .Call().
Linking of all the object files into a dynamic library that R can load.
Loading the library and the R wrapper functions into R.
All this is automated via sourceCpp() or when using Rcpp::compileAttributes() in the context of packages using Rcpp, c.f. the vignettes on attributes and packages.

C++, can't access included header files from so library

I try to include my self built .so library in the test.cpp file.
When I try to make the test.cpp file I get this exception:
root#airdrop:/home/pi/naza-interface/examples# make
g++ -c test.cpp
test.cpp:31:35: fatal error: naza_interface_manual.h: No such file or
directory
#include "naza_interface_manual.h"
^
compilation terminated.
Makefile:5: recipe for target 'test.o' failed
make: *** [test.o] Error 1
The Makefile for test.cpp looks like that:
output: test.o
g++ -L. -lnazainterface -o test test.o
test.o:
g++ -c test.cpp
clean:
rm -f *.o
test.cpp just includes the library.
#include "naza_interface_manual.h"
The library contains two files, naza_interface_manual.h and naza_interface_manual.cpp. The library's makefile looks like that:
libso: naza_interface_manual.o pca9685.o
g++ -fPIC -L/usr/local/lib -shared naza_interface_manual.o
pca9685.o -lbcm2835 -o libnazainterface.so
naza_interface_manual.o: src/naza_interface_manual.cpp src/naza_interface_manual.h
g++ -fPIC -c -Wall src/naza_interface_manual.cpp
pca9685.o: src/PCA9685/pca9685.cpp src/PCA9685/pca9685.h
g++ -c src/PCA9685/pca9685.cpp
install: naza_interface_manual.o pca9685.o
g++ -L/usr/local/lib naza_interface_manual.o pca9685.o -lbcm2835 -
shared -o /usr/local/libnazainterface.so
clean:
rm *.o output
naza_interface_manual.h:
#ifndef NAZA_INTERFACE_MANUAL_H_
#define NAZA_INTERFACE_MANUAL_H_
class naza_interface_manual_c{
public:
naza_interface_manual_c();
// A: For roll control (left/right)
// E: For pitch control (front/back)
// T: For throttle control
// R: For rudder control
// U: For Control Model Switch
void configure_pins(int A, int E, int T, int R, int U);
void fly_forward(int speed);
void fly_backward(int speed);
void fly_up(int speed);
void fly_down(int speed);
void fly_left(int speed);
void fly_right(int speed);
};
#endif
naza_interface_manual.cpp:
#include <iostream>
#include <wiringPi.h>
#include <stdio.h>
#include <stdint.h>
#include <unistd.h>
#include "naza_interface_manual.h"
#include "PCA9685/pca9685.h"
naza_interface_manual_c::naza_interface_manual_c(){
std::cout << "Starting Naza Interface";
}
void naza_interface_manual_c::configure_pins(int A, int E, int T, int R, int U){
PCA9685 pca9685;
pca9685.SetFrequency(100);
pca9685.Write(CHANNEL(0), VALUE(350));
}
void naza_interface_manual_c::fly_forward(int speed){
}
void naza_interface_manual_c::fly_backward(int speed){
}
void naza_interface_manual_c::fly_up(int speed){
}
void naza_interface_manual_c::fly_down(int speed){
}
void naza_interface_manual_c::fly_left(int speed){
}
void naza_interface_manual_c::fly_right(int speed){
}
Your Makefile doesn't install the header file. In fact, it also installs the shared object in a non-standard location: /usr/local. You want the library to go into /usr/local/lib and you need the header file installed in /usr/local/include.
Your Makefile is not consistent with conventional rules: You have no all rule, you are creating the library directly in the installation directory, instead of calling /usr/bin/install... I suggest you look into "proper" Makefile layout, if you want to distribute this. Users expect a lot of things from the Makefiles you give them; there are de-facto standards to follow.
If you want to use the library without having installed it, you need to provide the compiler the relevant include directive in your test.o: target; something like -Ipath/to/your/header.
Your compilation doesn't give the compiler the include path to find the header.
Instead, specify a base location and add the path to the compile. Otherwise if you can change the naza interface library, its install target should install the headers to a system (or $PREFIX/include) location.
test.o:
g++ -I$(NAZA_INTERFACE_LIB)/src/ -c test.cpp

undefined reference to `function_name'

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.

Undefined reference to a class method [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have one class, Pose.hpp and Pose.cpp, and a main.cpp to use this, but when compile I'm getting error.
I'm using this to compile:
$g++ -c main.cpp
$g++ -c Pose.cpp
$g++ -o Pose.o main.o
// Pose.hpp
#include "Vec3f.hh"
#include <vector>
using namespace std;
using std::vector;
class Pose
{
Vec3f root_position;
vector < Vec3f > bonesAngles;
vector < Vec3f > bonesPosition;
public:
void setRootPosition (Vec3f position);
void addBone (Vec3f newBone);
};
//Pose.cpp
#include "Pose.hpp"
using namespace std;
void Pose :: setRootPosition (Vec3f position)
{
root_position = position;
}
void Pose :: addBone (Vec3f newBone)
{
bonesAngles.push_back(newBone);
}
//main.cpp
#include <iostream>
#include "Pose.hpp"
using namespace std;
int main()
{
Pose pose;
Vec3f aux(.2,.3,.4);
pose.addBone(aux);
return 0;
}
I got the error:
/tmp/ccSzsctS.o:main.cpp:function main: error: undefined reference to 'Pose::addBone(Vec3f)'
collect2: erro: ld returned 1 exit status
You must compile a .o file for each .cpp. Try this :
$g++ -c Pose.cpp -o Pose.o
$g++ -c main.cpp -o main.o
$g++ -o Pose.o main.o MyProgram
The .o files are called "object" files, there will contain basically all the information for final compilation. In your case, you need the functions definitions (it's what your compile error says). Compiling as you did only checks that the syntax is correct.
There is 2 steps during compiling : compiling and linking. Compiling (with your command lines) is only checking that the syntax is correct basically. At the end of this step the object files are produced. Linking is using the object files (and possibly other types of files, like .dll, .lib) to create a final product (in your case an executable file, but you can produce many different other things, like .dll, .lib, .a, etc ...).
Understanding compilation is a critical step in programming, I invite you to get into it.
For a more detailed explanation, have a look at : http://www.cs.fsu.edu/~jestes/howto/g++compiling.txt
Good luck !

runtime initialization a const

I have a header (only) file constants.h, where I define all the constant variables, to be used later in the library. However, there is one variable, which I would like to define run-time in an implementation file. I tried to do something like this:
constant.hpp
extern const unsigned int numTests;
somewhere else in run.cpp
const unsigned int numTests = 10;
and, then yet another file tester.cpp uses
if ( n < numTests) {
// do something
}
Now, when I compile it, I get a linker error in tester.o as undefined symbol numTests. I sort of understand why this is happening: the tester.cpp includes constants.hpp and not the run.cpp and so, it can not find the constant numTests initialized in run.cpp.
Is there any better way to do it?
TIA,
Nikhil
Make sure you are compiling both run.cpp and tester.cpp when you compile your program and you won't get a linker error.
You need to link run.o when creating the executable:
g++ -o tester tester.cpp run.o ; for GNU C++
(Check your own compiler's command line switches if you're not using GNU C++)