Cern ROOT XCode IDE does not compile - xcode

I followed the Setup Guide of another Post and got a working example using XCode as ROOT IDE. Now I tried to include the Minuit2 example code within the same project:
#include "Minuit2/Minuit2Minimizer.h"
#include "Math/Functor.h"
double RosenBrock(const double *xx )
{
const double_t x = xx[0];
const double_t y = xx[1];
const double_t tmp1 = y-x*x;
const double_t tmp2 = 1-x;
return 100*tmp1*tmp1+tmp2*tmp2;
}
int main()
{
// Choose method upon creation between:
// kMigrad, kSimplex, kCombined,
// kScan, kFumili
ROOT::Minuit2::Minuit2Minimizer min ( ROOT::Minuit2::kMigrad );
min.SetMaxFunctionCalls(1000000);
min.SetMaxIterations(100000);
min.SetTolerance(0.001);
ROOT::Math::Functor f(&RosenBrock,2);
double step[2] = {0.01,0.01};
double variable[2] = { -1.,1.2};
min.SetFunction(f);
// Set the free variables to be minimized!
min.SetVariable(0,"x",variable[0], step[0]);
min.SetVariable(1,"y",variable[1], step[1]);
min.Minimize();
const double *xs = min.X();
std::cout << "Minimum: f(" << xs[0] << "," << xs[1] << "): "
<< RosenBrock(xs) << std::endl;
return 0;
}
I tested the code as a root macro and it worked fine, but trying to compile it within XCode yields the following errors:
Undefined symbols for architecture x86_64:
defined symbols for architecture x86_64:
"ROOT::Minuit2::Minuit2Minimizer::SetFunction(ROOT::Math::IBaseFunctionMultiDim const&)", referenced from:
_main in main.o
and serveral more undefined symbols.
What did I do wrong?
I installed ROOT with Macports via
sudo port install root6
and made sure that the variant +minuit2 is installed. Then I added the Search Paths and Linking:
Targets > Build Settings > User Header Search Paths > Debug > /opt/local/libexec/root6/include/root
Targets > Build Settings > Other Linker Flags > Debug > -L/opt/local/libexec/root6/lib/root -lCore -lRIO -lNet -lHist -lGraf -lGraf3d -lGpad -lTree -lRint -lPostscript -lMatrix -lPhysics -lMathCore -lThread -lMultiProc -lpthread -Wl,-rpath,/opt/local/libexec/root6/lib/root -stdlib=libc++ -lm -ldl
which I got from
root/bin/root-config --libs

To answer this Question myself:
You have to add
-lMinuit2
to the other Flags in your Build settings.
Somehow
root-config --libs
does not automatically add the minuit2 linker.

Related

tensorflow c++ error: Unary VariantShapeFn for type_name: int already registered

i compliled tensorflow with bazel successfully and got libtensorflow_cc.so and libtensorflow_framework.so. Then i built a c++ binary with those share libraries successfully, but errors occurred when i run the binary, info is like:
F tensorflow/core/framework/variant_op_registry.cc:51] Check failed: existing == nullptr (0x24fc538 vs. nullptr)Unary VariantShapeFn for type_name: int already registered
c++ code:
#include "tensorflow/cc/client/client_session.h"
#include "tensorflow/cc/ops/standard_ops.h"
#include "tensorflow/core/framework/tensor.h"
int main() {
using namespace tensorflow;
using namespace tensorflow::ops;
Scope root = Scope::NewRootScope();auto A = Const(root, { {3.f, 2.f}, {-1.f, 0.f} });
// // Vector b = [3 5
auto b = Const(root, { {3.f, 5.f} });
auto v = MatMul(root.WithOpName("v"), A, b, MatMul::TransposeB(true));
std::vector<Tensor> outputs;
ClientSession session(root);
// Run and fetch v
TF_CHECK_OK(session.Run({v}, &outputs));
// Expect outputs[0] == [19; -3]
LOG(INFO) << outputs[0].matrix<float>();
return 0;
}
i got my c++ binary using cmd:
g++ -std=c++11 test.cpp -o test -I./include -L./lib -L/home/work/chengjy/tools/protobuf/lib -I/home/work/chengjy/tools/tensorflow/eigen/eigen/eigen-eigen-fd6845384b86 -I/home/work/chengjy/tools/protobuf/include -ltensorflow_cc -ltensorflow_framework -lprotobuf -lpthread -ldl -O3 -Wall
did anyone have this problem before?
it seems that i fixed this problem by removing -ltensorflow_framework from the compile cmd.

Fail compilation with FFTW3 library with g++-6.4.0 under cygwin

I have the following code from sigpack website
#include "sigpack.h"
using namespace arma;
using namespace sp;
int main()
{
int N = 45191; // Large prime number
FFTW ss(N,FFTW_ESTIMATE);
vec x(N);
cx_vec Sxx(N);
vec P(N);
clock_t tic;
for (int n = 1; n < 5; n++)
{
x.randn();
tic = clock();
Sxx = ss.fft(x);
cout << "FFTW Loop[" << n << "]. Time = " << (clock() - tic)/
double(CLOCKS_PER_SEC) << endl;
x.randn();
tic = clock();
Sxx = fft(x);
cout << " FFT Loop[" << n << "]. Time = " << (clock() - tic) /
double(CLOCKS_PER_SEC) << endl;
}
return 0;
}
I save it as exFFT1
when i build the file under cygwin with g++-6.4.0with command :
g++ -Wall -std=gnu++11 -I/home/budiarjo/sigpack-1.2.4/sigpack/ -I/usr/local/include/ -I/opt/OpenBLAS/include/ -L/opt/OpenBLAS/lib/ -L/usr/local/lib/ -DHAVE_FFTW -lblas -llapack -Dunix exFFT1.cpp -lfftw3 -o exFFT1
The following are the error messages :
/tmp/ccRsp1Bf.o:exFFT1.cpp:(.text.startup+0x1cc): undefined reference to `__emutls_v._ZN4arma23arma_rng_cxx11_instanceE'
/tmp/ccRsp1Bf.o:exFFT1.cpp:(.text.startup+0x242): undefined reference to `__emutls_v._ZN4arma23arma_rng_cxx11_instanceE'
/tmp/ccRsp1Bf.o:exFFT1.cpp:(.text.startup+0x6db): undefined reference to `__emutls_v._ZN4arma23arma_rng_cxx11_instanceE'
/tmp/ccRsp1Bf.o:exFFT1.cpp:(.text.startup+0x752): undefined reference to `__emutls_v._ZN4arma23arma_rng_cxx11_instanceE'
/tmp/ccRsp1Bf.o:exFFT1.cpp:(.text.startup+0x1662): undefined reference to `__emutls_v._ZN4arma23arma_rng_cxx11_instanceE'
/tmp/ccRsp1Bf.o:exFFT1.cpp:(.text.startup+0x16f9): more undefined references to `__emutls_v._ZN4arma23arma_rng_cxx11_instanceE' follow
collect2: error: ld returned 1 exit status
But in Raspberry Pi with g++-4.8, when i build the file with the command
sudo g++ -Wall -std=gnu++11 -I/home/pi/sigpack-1.2.4/sigpack/ -I/opt/OpenBLAS/include/ -L/opt/OpenBLAS/lib/ -DHAVE_FFTW -lopenblas -llapack -Dunix exFFT1.cpp -lfftw3 -o exFFT1
I could build successfully without any error messages.
What could be wrong with my cygwin setting?
Should I replace the g++-6.4.0 in cygwin with g++-4.80? How to downgrade it, since if I compile and do make, the g++-4.80 package, I would get error messages as well because of the usge of gcc/g++-6.40 during the process.

C++ linker error after overloading an operator

I keep getting the following error when I try to compile an example program from C++ how to program, Deitel and Deitel. I used g++ Fig11_05.cpp -o Fig11_05
I spent hours trying to solve this problem by looking up the Internet and particularly stackoverflow, but with no avail!
I tried using different command line arguments such as -libstd=libc++, -std=c++11, -std=c++14
the error I keep getting is as this:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from:
_main in Fig11_05-1f04bd.o
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from:
_main in Fig11_05-1f04bd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The result of g++ -v:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The codes:
// Fig. 11.3: PhoneNumber.h
// PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
#include <string>
class PhoneNumber
{
friend std::ostream& operator<<(std::ostream&, const PhoneNumber&);
friend std::istream &operator>>(std::istream&, PhoneNumber&);
private:
std::string areaCode; // 3-digit area code
std::string exchange; // 3-digit exchange
std::string line; // 4-digit line
}; // end class PhoneNumber
#endif
// Fig. 11.4: PhoneNumber.cpp
// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber.
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
// overloaded stream insertion operator; cannot be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream& operator<<(ostream& output, const PhoneNumber& number)
{
output << "Area Code: " << number.areaCode << "\nExchange: "
<< number.exchange << "\nLine: " << number.line << "\n"
<< "(" << number.areaCode << ") " << number.exchange << "-"
<< number.line << "\n";;
return output; // enables cout << a << b << c;
} // end function operator<<
// overloaded stream extraction operator; cannot be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber;
istream& operator>>(istream& input, PhoneNumber& number)
{
input.ignore(); // skip (
input >> setw(3) >> number.areaCode; // input area code
input.ignore(2); // skip ) and space
input >> setw(3) >> number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >> setw(4) >> number.line; // input line
return input; // enables cin >> a >> b >> c;
} // end function operator>>
// Fig. 11.5: fig11_05.cpp
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators.
#include <iostream>
#include "PhoneNumber.h"
using namespace std;
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the global function call operator>>( cin, phone )
cin >> phone;
cout << "\nThe phone number entered was:\n";
// cout << phone invokes operator<< by implicitly issuing
// the global function call operator<<( cout, phone )
cout << phone << endl;
} // end main
Note: I recently installed then uninstalled CUDA toolkit 8. I needed a newer version of Xcode, so I installed Xcode the newest version 8.2.1, and kept the old version in a different directory just in case. I don't think the problem is with the installation of Xcode though. Also when I installed CUDA I had to set DYLD_LIBRARY_PATH to a directory. However, it might not be the source of the problem! I am just trying to help you figure out the problem to help me fix it :)
Thank you in advance! You are such a good community!
Your attempt to build the program:
g++ Fig11_05.cpp -o Fig11_05
is unsuccessful because you have neglected to compile and link into the
program the code that contains the definitions of the functions:
std::ostream& operator<<(std::ostream& output, const PhoneNumber& number)
and:
std::istream& operator>>(std::istream& input, PhoneNumber& number)
which are called by your program. That is why the linker says:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from: _main in Fig11_05-1f04bd.o
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from: _main in Fig11_05-1f04bd.o
...
Do this instead:
g++ Fig11_05.cpp PhoneNumber.cpp -o Fig11_05
Or to spell out the process in full:-
Compile source file Fig11_05.cpp to object file Fig11_05.o:
$ g++ -o Fig11_05.o -c Fig11_05.cpp
Compile source file PhoneNumber.cpp to object file PhoneNumber.o:
$ g++ -o PhoneNumber.o -c PhoneNumber.cpp
Link the object files Fig11_05.o and PhoneNumber.o into program Fig11_05
$ g++ -o Fig11_05 Fig11_05.o PhoneNumber.o
You can then run:
./Fig11_05
Here is a fairly good beginner's tutorial
about building C or C++ programs with GCC.

matlab mex clang C++11 thread -> Undefined symbols error

Goal: I want to use thread STL of C++11 in Matlab mex file (R2013a) using Xcode 4.6
I modified ~/.matlab/R2013a/mexopts.sh
CC='clang++' # was llvm-gcc-4.2
CXX='clang++' # was llvm-g++-4.2
MACOSX_DEPLOYMENT_TARGET='10.8' # was 10.5. C++11 is supported >=10.7
CXXFLAGS="$CXXFLAGS -std=gnu++11 -stdlib=libc++" # additional flags
Normal mex files without C++11 features are compiled well. Further, STL is well detected by the compiler except linking failure.
>> mex mextest.cpp
Undefined symbols for architecture x86_64:
"std::__1::__thread_struct::__thread_struct()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__thread_struct::~__thread_struct()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__thread_local_data()", referenced from:
void* std::__1::__thread_proxy<std::__1::tuple<void (*)()> >(void*) in mextest.o
"std::__1::__throw_system_error(int, char const*)", referenced from:
_mexFunction in mextest.o
"std::__1::thread::join()", referenced from:
_mexFunction in mextest.o
"std::__1::thread::~thread()", referenced from:
_mexFunction in mextest.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
mex: link of ' "mextest.mexmaci64"' failed.
Error using mex (line 206)
Unable to complete successfully.
The actual source code is shown below. The details are not important because it compiles well in Matlab R2013 WINDOWS version with Visual Studio 2012 Express. An equivalent cpp was also well compiled with "clang++ -std=gnu++11 -stdlib=libc++ clangtest.cpp". So, at least, there is no logical error in the codes (I'm not saying it is safe codes. It is just a test.)
#include "mex.h"
#include <thread>
#include <stdio.h>
int count_thread1 = 0;
int count_thread2 = 0;
void hello()
{
count_thread2 = 0;
for(int i=0; i<=10000; i++){
for (int j=1;j<=20000;j++){
count_thread2 = i-j-1;
}
count_thread2++;
printf("2: %d , %d\n", count_thread1, count_thread2); // Not sure if printf is thread-safe in Matlab. But it works in this particular example
}
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
count_thread1 = 0;
std::thread t(hello);
for (int i=1;i<=10000;i++)
{
for (int j=1;j<=20000;j++){
count_thread1 = -i+j-1;
}
count_thread1++;
mexPrintf("1: %d , %d\n", count_thread1, count_thread2);
}
mexPrintf("\n");
t.join();
mexPrintf("Done\n");
}
It seems like I have to replace some include directories and/or library directories. What kind of options should be modify?
Thank you.
The error is due to compiling against -stdlib=libc++ but linking against -lstdc++. You can fix it in one of two ways:
Fix it in mexopts.sh. The most drastic and effective solution. Located in ~/.matlab/${MATLAB_VERSION}/mexopts.sh, this determines all compiler options. Simply find/replace all stdc++ to c++.
Patchwork solution: Simply add -lc++ to the tail end of CXXLIBS. I'm not sure what the effect of linking against multiple versions of the standard libraries is, but it seems to work. In your mex invocation, add the argument CXXLIBS="\$CXXLIBS -lc++".
As a secondary issue, I believe you're completely overwriting the value of CXXFLAGS; you must escape the $ symbol as I did above with the libraries.

Compiling C++ that uses Boost::mpi with Xcode 4

I'm trying to run the following simple example from Xcode4:
#include <boost/mpi/environment.hpp>
#include <boost/mpi/communicator.hpp>
#include <iostream>
namespace mpi = boost::mpi;
int main(int argc, char* argv[])
{
mpi::environment env(argc, argv);
mpi::communicator world;
std::cout << "I am process " << world.rank() << " of " << world.size()
<< "." << std::endl;
return 0;
}
I've added libboost_mpi and libboost_serialization to Xcode, and compiling using the default LLVM returns :
/usr/local/include/boost/mpi/communicator.hpp:1329:9: error: call to
implicitly-deleted copy constructor of 'boost::mpi::communicator'
: comm(comm), source(source), tag(tag), ia(comm), value(value)
^ ~~~~
However, I can compile and run using
mpic++ -I/usr/local/include main.cpp -L/usr/local/lib
-lboost_mpi -lboost_serialization
Although mpic++ seems to be calling through to LLVM:
$ mpic++
i686-apple-darwin11-llvm-g++-4.2: no input files
Anyways, I tried adding mpic++ as a compiler option in Xcode 4. I can run
$ sudo opensnoop -n Xcode | grep mpicc.xcspec
and see that the spec file is being loaded by Xcode, but I don't see any MPICC option. My spec file is fairly simple:
/**
Xcode Compiler Specification for MPICC
*/
{ Type = Compiler;
Identifier = com.apple.compilers.mpicc;
BasedOn = com.apple.compilers.gcc.4_2;
Name = “MPICC”;
Version = “Default”;
Description = “MPI GNU C/C++ Compiler 4.0″;
ExecPath = “/usr/local/bin/mpicc”;
PrecompStyle = pch;
}
and it's stored in
/Applications/Xcode.app/Contents/PlugIns/Xcode3Core.ideplugin/Contents/SharedSupport/Developer/Library/Xcode/Plug-ins/LLVM GCC 4.2.xcplugin/Contents/Resources/mpicc.xcspec
So this works:
link binary with:
libmpi_cxx.dylib
libmpi.dylib
libboost_mpi.dylib
libboost_serialization.dylib
Change compiler (under build options) to LLVM GCC 4.2 (hinted at by running mpic++ directly, which reports that it's using llvm gcc 4.2 internally)
Under targets, build phases, compile sources, add the compiler option "-lm" to report that you need to link with libm. Credit to #pyCthon for pointing out mpic++ --showme:link which revealed the final library that was allowing it to build successfully from the command line

Resources