Hello,
I would like to be able to compile a root macro so that a user with just the correct root libraries can run it through a command like ./Sum
I don’t know if this is possible and am having difficulty tracking down instructions and tutorials concerning compiling scripts within root.
I took a very simple summation code from a website example:
Sum.C
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void sum() {
int max=0;
cout << "Type in upper limit ";
cin >> max;
int sum = 0;
for ( int i = 0; i <= max; ++i) {
sum += i;
}
cout << "The sum of 1.." << max << " is " << sum << endl;
}
And I have attempted to compile it through my normal c++ method but receive the errors:
herbie@herbie-laptop:~/Documents/root/rootinput$ gcc -o sum sum.C
/usr/lib/gcc/i486-linux-gnu/4.4.1/…/…/…/…/lib/crt1.o: In function _start': /build/buildd/eglibc-2.10.1/csu/../sysdeps/i386/elf/start.S:115: undefined reference to
main’
/tmp/ccwUsCsH.o: In function sum()': sum.C:(.text+0x19): undefined reference to
std::cout’
sum.C:(.text+0x1e): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' sum.C:(.text+0x2c): undefined reference to
std::cin’
sum.C:(.text+0x31): undefined reference to std::basic_istream<char, std::char_traits<char> >::operator>>(int&)' sum.C:(.text+0x6a): undefined reference to
std::cout’
sum.C:(.text+0x6f): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' sum.C:(.text+0x7b): undefined reference to
std::basic_ostream<char, std::char_traits >::operator<<(int)'
sum.C:(.text+0x8b): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' sum.C:(.text+0x9a): undefined reference to
std::basic_ostream<char, std::char_traits >::operator<<(int)‘
sum.C:(.text+0xa2): undefined reference to std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)' sum.C:(.text+0xaa): undefined reference to
std::basic_ostream<char, std::char_traits >::operator<<(std::basic_ostream<char, std::char_traits >& (*)(std::basic_ostream<char, std::char_traits >&))’
/tmp/ccwUsCsH.o: In function __static_initialization_and_destruction_0(int, int)': sum.C:(.text+0xd1): undefined reference to
std::ios_base::Init::Init()'
sum.C:(.text+0xd6): undefined reference to std::ios_base::Init::~Init()' /tmp/ccwUsCsH.o:(.eh_frame+0x12): undefined reference to
__gxx_personality_v0’
collect2: ld returned 1 exit status
herbie@herbie-laptop:~/Documents/root/rootinput$
Any help would be fantastic. I’m afraid that I’m fairly new to root, so I dont know much of the technical details, but I’d very much like to get this to work.
Many Thanks.
Geoff Herbert
PS, Am I also correct in thinking that if I wanted to take the input from the command line, then I would write the code as:
Sum.C
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void sum(int max=0) {
int sum = 0;
for ( int i = 0; i <= max; ++i) {
sum += i;
}
cout << "The sum of 1.." << max << " is " << sum << endl;
}
Thanks.