Compiling Trilino - c++

I'm trying to compile a simple test program using the Trilino package but something is wrong. The install of Trilino have went fine as far as I know but there must be something wrong with the linking or something. Below is my makefile:
include /home/jacob/Trilinos/trilinos-build/Makefile.export.Trilinos
CXX=$(Trilinos_CXX_COMPILER)
CC=$(Trilinos_C_COMPILER)
FORT=/usr/bin/gfortran
CXX_FLAGS=$(Trilinos_CXX_COMPILER_FLAGS) $(USER_CXX_FLAGS)
C_FLAGS=$(Trilinos_C_COMPILER_FLAGS) $(USERC_FLAGS)
FORT_FLAGS=$(Trilinos_Fortran_COMPILER_FLAGS) $(USER_FORT_FLAGS)
INCLUDE_DIRS=$(Trilinos_INCLUDE_DIRS) $(Trilinos_TPL_INCLUDE_DIRS)
LIBRARY_DIRS=$(Trilinos_LIBRARY_DIRS) $(Trilinos_TPL_LIBRARY_DIRS)
LIBRARIES=$(Trilinos_LIBRARIES) $(Trilinos_TPL_LIBRARIES)
LINK_FLAGS=$(Trilinos_EXTRA_LD_FLAGS)
DEFINES=-DMYAPP_EPETRA
default: print_info vector.x
vector.x: libmyappLib.a
$(CXX) $(CXX_FLAGS) libMyappLib.a -o vector.x $(LINK_FLAGS) $(INCLUDE_DIRS) $(DEFINES) $(LIBRARY_DIRS) $(LIBRARIES)
libmyappLib.a: es.o
$(Trilinos_AR) cr libMyappLib.a es.o
es.o:
$(CXX) -c $(CXX_FLAGS) $(INCLUDE_DIRS) $(DEFINES) es.cpp
And here is the program:
#include "Epetra_SerialComm.h"
#include "Epetra_Map.h"
#include "Epetra_Vector.h"
#include "Epetra_Version.h"
int main(int argc, char *argv[])
{
std::cout << Epetra_Version() << std::endl << std::endl;
Epetra_SerialComm Comm;
int NumElements = 1000;
Epetra_Map Map(NumElements, 0, Comm);
Epetra_Vector x(Map);
Epetra_Vector b(Map);
b.Random();
x.Update(2.0, b, 0.0); // x = 2*b
double bnorm, xnorm;
x.Norm2(&xnorm);
b.Norm2(&bnorm);
std::cout << "2 norm of x = " << xnorm << std::endl
<< "2 norm of b = " << bnorm << std::endl;
return 0;
}
However, when running make it just gives back:
es.cpp:(.text.startup+0x91): undefined reference to Epetra_SerialComm::Epetra_SerialComm()
es.cpp:(.text.startup+0xa7): undefined reference to `Epetra_Map::Epetra_Map(int,int, Epetra_Comm const&)'
es.cpp:(.text.startup+0xbb): undefined reference to `Epetra_Vector::Epetra_Vector(Epetra_BlockMap const&, bool)'
.
.
.
collect2: error: ld returned 1 exit status
make: *** [vector.x] Fel 1
For every piece of code inside main, so it seems like it cannot find the Epetra package even though itś not complaining about the includes. Does anyone have a clue of what might be the problem? I'm fairly new to C++/C and handling the Trilino package is rather complicated so any tip is highly appreciated.

Related

Octave from QT: undefined reference

I would like to run Octave script from Qt GUI application.
Here is .pro file:
...
win32 {
INCLUDEPATH += c:/Octave/Octave-4.2.1/include/octave-4.2.1/octave
LIBS += c:/Octave/Octave-4.2.1/lib/octave/4.2.1/liboctave.dll.a \
c:/Octave/Octave-4.2.1/lib/octave/4.2.1/liboctinterp.dll.a
DEPENDPATH += c:/Octave/Octave-4.2.1/bin
}
...
Here is .cpp file (example is taken from docs):
#include <iostream>
#include <octave/oct.h>
#include <octave/octave.h>
#include <octave/parse.h>
#include <octave/interpreter.h>
int
main (void)
{
string_vector argv (2);
argv(0) = "embedded";
argv(1) = "-q";
octave_main (2, argv.c_str_vec (), 1);
octave_idx_type n = 2;
octave_value_list in;
for (octave_idx_type i = 0; i < n; i++)
in(i) = octave_value (5 * (i + 2));
octave_value_list out = feval ("gcd", in, 1);
if (out.length () > 0)
std::cout << "GCD of ["
<< in(0).int_value ()
<< ", "
<< in(1).int_value ()
<< "] is " << out(0).int_value ()
<< std::endl;
else
std::cout << "invalid\n";
clean_up_and_exit (0);
}
When I am trying to compile C++ code from Qt Creator I have the following error:
undefined reference to feval(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, octave_value_list const&, int)
I also have the following errors from compiler:
undefined reference to Array<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::nil_rep()
undefined reference to Array<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::resize_fill_value() const
undefined reference to Array<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >::resize_fill_value() const
but when I Ctrl+Click on Array or feval - Qt Creator opens the appropriate files.
I can also compile the example c++ file using the following command from Octave GUI:
mkoctfile --link-stand-alone embedded.cc -o embedded
What library/path should I add?
Thank you very much in advance.
If you run mkoctfile on your example from above (I copied it into main.cc) with verbose, you'll see all flags needed:
mkoctfile -v --link-stand-alone main.cc
g++ -std=gnu++11 -c -fPIC -I/usr/local/include/octave-4.2.0/octave/.. -I/usr/local/include/octave-4.2.0/octave -I/usr/local/include -pthread -fopenmp -g -O2 main.cc -o main.o
g++ -std=gnu++11 -I/usr/local/include/octave-4.2.0/octave/.. -I/usr/local/include/octave-4.2.0/octave -I/usr/local/include -pthread -fopenmp -g -O2 -rdynamic -fPIC main.o -L/usr/local/lib/octave/4.2.0 -L/usr/local/lib -loctinterp -loctave
Now you have to add these to your QtCreator. (I guess -std=gnu++11 is missing)
It seems from the error that gcd function does not exist, or it cannot find reference to it. I don't know about gcd function whether it is part of octave or it is your custom defined, but surely it's missing a reference to it.

Undefined reference to (error) in C++ Eclipse but working in Visual Studio 2015

I am trying to integrate AMPL with C/C++ using AMPL-API on Windows-7 in Eclipse Mars 2.0. I created a Makefile project in Eclipse which uses MinGW CC to compile the firstexample code given in their example directory.
firstexample.cpp:
#include <iostream>
#include "ampl/ampl.h"
using namespace std;
int main() {
ampl::AMPL ampl;
// Read the model and data files.
std::string modelDirectory = "models";
ampl.read(modelDirectory + "/diet/diet.mod");
ampl.readData(modelDirectory + "/diet/diet.dat");
// Solve
ampl.solve();
// Get objective entity by AMPL name
ampl::Objective totalcost = ampl.getObjective("total_cost");
// Print it
std::cout << "Objective is: " << totalcost.value() << std::endl;
// Get objective entity by AMPL name
ampl::Objective totalcost = ampl.getObjective("total_cost");
// Print it
std::cout << "Objective is: " << totalcost.value() << std::endl;
// Reassign data - specific instances
ampl::Parameter cost = ampl.getParameter("cost");
cost.setValues(new Tuple[2]{ ampl::Arg("BEEF"), ampl::Arg("HAM")}, new Arg[2]{ 5.01, 4.55 },
2);
std::cout << "Increased costs of beef and ham." << std::endl;
// Resolve and display objective
ampl.solve();
std::cout << "New objective value: " << totalcost.value() << std::endl;
// Reassign data - all instances
ampl::Arg elements[8]{ 3, 5, 5, 6, 1, 2, 5.01, 4.55 };
cost.setValues(elements);
std::cout << "Updated all costs." << std::endl;
// Resolve and display objective
ampl.solve();
std::cout << "New objective value: " << totalcost.value() << std::endl;
// Get the values of the variable Buy in a dataframe object
Variable buy = ampl.getVariable("Buy");
ampl::DataFrame df;
df = buy.getValues();
// Print them
df.print();
ampl::DataFrame df2;
// Get the values of an expression into a DataFrame object
df2 = ampl.getData("{j in FOOD} 100*Buy[j]/Buy[j].ub");
// Print them
df2.print();
}
Following is my Makefile:
CC = g++
CFLAGS = -O2 -g -Wall -fmessage-length=0
INCLUDES = -I "C:\\Local\\AMPL\\AMPL32\\amplapi32\\include"
OBJS = AMPL.o
LFLAGS = -L "C:\\Local\\AMPL\\AMPL32\\amplapi32\\lib"
LIBS = -lampl1.2.2
TARGET = AMPL.exe
$(TARGET): $(OBJS)
$(CC) $(CFLAGS) $(INCLUDES) -o $(TARGET) $(OBJS) $(LFLAGS) $(LIBS)
AMPL.o: AMPL.cpp
$(CC) $(CFLAGS) $(INCLUDES) -c AMPL.cpp
all: $(TARGET)
clean:
rm -f $(OBJS) $(TARGET)
I have added path of required dll files (libampl1.2.2.dll) to the environment variables. I am able to compile and execute code on Visual Studio 2015 with two minor changes:
Without using Makefile (It is a Win32 Console Application)
Adding #include "stdafx.h" in firstexample.cc
However when I execute the same code in Eclipse, it gives me following error:
src\AMPLTesting.o: In function `ZN4ampl8internal11deleteTupleERNS0_5TupleE':
C:/Local/AMPL/AMPL32/amplapi32/include/ampl/ep/tuple_ep.h:24: undefined reference to `_imp___ZN4ampl8internal24AMPL_Variant_DeleteArrayEPKNS0_7VariantE'
src\AMPLTesting.o: In function `ZN4ampl8internal12TupleBuilderC1Ej':
C:/Local/AMPL/AMPL32/amplapi32/include/ampl/ep/tuple_ep.h:35: undefined reference to `_imp___ZN4ampl8internal24AMPL_Variant_CreateArrayEjPNS0_16ErrorInformationE'
collect2.exe: error: ld returned 1 exit status
I am not sure what is the problem? Am I missing some command line option in the Makefile or not adding any specific library? Please help me with this.
The beta version of the C++ API only supports MSVC on Windows at the moment. Support for other compilers will be added in future releases.

C++ and R: Create a .so or .dll

I have doubt. I know that it is possible to call from a cppfile an Rfunction.
But it is possible to compile that cppfile (that has an R function inside -from a package, for example caret-) into a .soor dll?
If it is possible. How does having a chuck of Rcode inside works. Does the compiled code calls the R interpreter before it compiles or it does not need to?
Thanks in advance
Yes, you can embed R inside of C or C++ application. The API is old, stable, somewhat documented and a little unwieldy. But it can be done.
Or you just use RInside which does everything for you -- and comes with eight (8) different example subdirectories containing dozens of worked and working examples. Here is (the core) of one (which is a little old, we may write it tighter now):
#include <RInside.h> // for the embedded R via RInside
#include <iomanip>
int main(int argc, char *argv[]) {
RInside R(argc, argv); // create an embedded R instance
std::string txt = // load library, run regression, create summary
"suppressMessages(require(stats));"
"swisssum <- summary(lm(Fertility ~ . , data = swiss));"
"print(swisssum)";
R.parseEvalQ(txt); // eval command, no return
// evaluate R expressions, and assign directly into Rcpp types
Rcpp::NumericMatrix M( (SEXP) R.parseEval("swcoef <- coef(swisssum)"));
Rcpp::StringVector cnames( (SEXP) R.parseEval("colnames(swcoef)"));
Rcpp::StringVector rnames( (SEXP) R.parseEval("rownames(swcoef)"));
std::cout << "\n\nAnd now from C++\n\n\t\t\t";
for (int i=0; i<cnames.size(); i++) {
std::cout << std::setw(11) << cnames[i] << "\t";
}
std::cout << std::endl;
for (int i=0; i<rnames.size(); i++) {
std::cout << std::setw(16) << rnames[i] << "\t";
for (int j=0; j<cnames.size(); j++) {
std::cout << std::setw(11) << M(i,j) << "\t";
}
std::cout << std::endl;
}
std::cout << std::endl;
exit(0);
}
Then:
edd#max:~/git/rinside/inst/examples/standard(master)$ make rinside_sample3
ccache g++ -I/usr/share/R/include -I/usr/local/lib/R/site-library/Rcpp/include -I/usr/local/lib/R/site-library/RInside/include -g -O2 -fstack-protector-strong -Wformat -Werror=format-security -D_FORTIFY_SOURCE=2 -g -O3 -Wall -pipe -Wno-unused -pedantic -Wall rinside_sample3.cpp -Wl,--export-dynamic -fopenmp -L/usr/lib/R/lib -lR -lpcre -llzma -lbz2 -lz -lrt -ldl -lm -lblas -llapack -L/usr/local/lib/R/site-library/RInside/lib -lRInside -Wl,-rpath,/usr/local/lib/R/site-library/RInside/lib -o rinside_sample3
edd#max:~/git/rinside/inst/examples/standard(master)$ ./rinside_sample3
Call:
lm(formula = Fertility ~ ., data = swiss)
Residuals:
Min 1Q Median 3Q Max
-15.2743 -5.2617 0.5032 4.1198 15.3213
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.91518 10.70604 6.250 1.91e-07 ***
Agriculture -0.17211 0.07030 -2.448 0.01873 *
Examination -0.25801 0.25388 -1.016 0.31546
Education -0.87094 0.18303 -4.758 2.43e-05 ***
Catholic 0.10412 0.03526 2.953 0.00519 **
Infant.Mortality 1.07705 0.38172 2.822 0.00734 **
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 7.165 on 41 degrees of freedom
Multiple R-squared: 0.7067, Adjusted R-squared: 0.671
F-statistic: 19.76 on 5 and 41 DF, p-value: 5.594e-10
And now from C++
Estimate Std. Error t value Pr(>|t|)
(Intercept) 66.9152 10.706 6.25023 1.90605e-07
Agriculture -0.172114 0.0703039 -2.44814 0.0187272
Examination -0.258008 0.253878 -1.01627 0.315462
Education -0.87094 0.183029 -4.75849 2.4306e-05
Catholic 0.104115 0.0352579 2.95297 0.00519008
Infant.Mortality 1.07705 0.38172 2.82157 0.00733572
edd#max:~/git/rinside/inst/examples/standard(master)$
which shows that
yes we can use R from C++
yes we can have R report the result
yes we can get them back to C++ too
As I said, there are dozens more examples. And no, it doesn't just magically compile R into your executable -- you need R installed and its shared libraries are called.

cpp-netlib complains about missing lboost-thread

I'm learning cpp-netlib and I tried running the exmaple client given on the official website. The code is very simple:
#include <boost/network/protocol/http/client.hpp>
#include <iostream>
int main(int argc, char *argv[]) {
using namespace boost::network;
if (argc != 2) {
std::cout << "Usage: " << argv[0] << " [url]" << std::endl;
return 1;
}
http::client client;
http::client::request request(argv[1]);
request << header("Connection", "close");
http::client::response response = client.get(request);
std::cout << body(response) << std::endl;
return 0;
}
And here is my makefile for this c++ application:
CC = g++ -std=c++11
CFLAG = -I/usr/local/Cellar/boost/1.57.0/include
LIBFLAG = -L/usr/local/Cellar/boost/1.57.0/lib
all: client
client: client.o
$(CC) $(LIBFLAG) -lboost_system -lboost_thread client.o -o client
client.o: client.cpp
$(CC) -c $(CFLAG) client.cpp
clean:
rm -rf *.o client
It complains about not finding lboost_thread library after compilation:
ld: library not found for -lboost_thread
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [client] Error 1
In my boost library directory, the boost_thread library shows up like this:
libboost_thread-mt.a libboost_thread-mt.dylib
Why isn't it able to find this library? Did I make any mistake in the linking?
Try changing your makefile to link to -lboost-thread-mt instead of -lboost-thread.
You seems to be missing libboost_thread for some reason

How to compile Apache Avro C++ example

Probably a newbie mistake, but can anyone please tell me what I'm doing wrong here?
Any help is much appreciated.
I wrote this simple Makefile:
CC=g++
INC=-I/usr/local/avro-cpp-1.7.2 -I/usr/local/boost_1_53_0
cpx : generated.cc
$(CC) -o cpx generated.cc $(INC)
Which generates these the errors:
g++ -o cpx generated.cc -I/usr/local/avro-cpp-1.7.2 -I/usr/local/boost_1_53_0
/tmp/ccYymUVo.o: In function `main':
generated.cc:(.text+0x84): undefined reference to `avro::memoryOutputStream(unsigned long)'
generated.cc:(.text+0xb0): undefined reference to `avro::binaryEncoder()'
generated.cc:(.text+0x11e): undefined reference to `avro::memoryInputStream(avro::OutputStream const&)'
generated.cc:(.text+0x150): undefined reference to `avro::binaryDecoder()'
collect2: ld returned 1 exit status
make: *** [cpx] Error 1
Here's the source as supplied in the Avro examples directory:
#############################
# cpx.hh
#############################
#ifndef CPX_HH_1278398428__H_
#define CPX_HH_1278398428__H_
#include "boost/any.hpp"
#include "avro/Specific.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
namespace c {
struct cpx {
double re;
double im;
};
}
namespace avro {
template<> struct codec_traits<c::cpx> {
static void encode(Encoder& e, const c::cpx& v) {
avro::encode(e, v.re);
avro::encode(e, v.im);
}
static void decode(Decoder& d, c::cpx& v) {
avro::decode(d, v.re);
avro::decode(d, v.im);
}
};
}
#endif
and
#############################
# generated.cc
#############################
#include "cpx.hh"
#include "avro/Encoder.hh"
#include "avro/Decoder.hh"
int
main()
{
std::auto_ptr<avro::OutputStream> out = avro::memoryOutputStream();
avro::EncoderPtr e = avro::binaryEncoder();
e->init(*out);
c::cpx c1;
c1.re = 1.0;
c1.im = 2.13;
avro::encode(*e, c1);
std::auto_ptr<avro::InputStream> in = avro::memoryInputStream(*out);
avro::DecoderPtr d = avro::binaryDecoder();
d->init(*in);
c::cpx c2;
avro::decode(*d, c2);
std::cout << '(' << c2.re << ", " << c2.im << ')' << std::endl;
return 0;
}
Thanks.
1)
in your output, you don't link against the avrocpp library, although your comments about the LD_LIBRARY_PATH suggest that you got the link to work, but the example program failed to find the library when executing. Your compile line needs the linker option: -l avrocpp
2)
If the avrocpp library .so is not installed in a path that your runtime linker is configured to search, you can add the library path into the binary itself with the linker option -rpath.
Example 1: Assuming your avrocpp library is installed in /usr/local/lib (i.e. /usr/local/lib/libavrocpp.so), and your runtime linker doesn't look in /usr/local/lib, add the rpath by adding this option to your compiler command line:
-Wl,-rpath,/usr/local/lib
Example 2: Assuming your avrocpp library is installed in /usr/local/avro-cpp-1.7.2/lib (i.e. /usr/local/avro-cpp-1.7.2/lib/libavrocpp.so), add the rpath by adding this option to your compiler command line:
-Wl,-rpath,/usr/local/avro-cpp-1.7.2/lib
Try using avrogencpp with options -i < schema_file > -o < header_file >
Download and compile the Avro c++ sources https://stackoverflow.com/questions/40889705/how-to-compile-apache-avro-c-on-windows (this will generate the "avrogencpp.exe")
Download avro-tools http://avro.apache.org/releases.html#Download, and (with java installed)
run:
java -jar avro-tools-1.8.1.jar idl file.avdl > file.json
Edit the generated json file and delete the following
If you have any lines preceding the required structs at the beginning, delete them. (I had "protocol": "file", "namespace" : "some_optional_namespace", "types" :)
messages entry (at the bottom)
If you have multiple structs defined in the same json file, surround them all with "[ ]" (you should have commas between the different structs and make sure the last struct as not proceeded with a ","
Run
avrogencpp.exe -p - -n some_optional_namespace -U --input file.json --output file.hh