Installing NR C++ Ubuntu 10.4


fugu
06-14-2010, 10:44 AM
Hi there,

I am experiencing difficulties with the NR makefile. The first lines of the output of the make command look like

gcc -I/usr/local/src/nr/recipes_cpp/utils -I. -o xairy.out -lm
xairy.o: In function `main':
xairy.cpp:(.text+0x2b): undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'

and many more undefined reference errors. I am new to make but I take it there is a problem with the linking?!

The head of my makefile is:


.SUFFIXES:
.SUFFIXES: .reslt .res .out .o .cpp
# Set the path and parameters below to the correct values when needed
NRROOT= /usr/local/src/nr/recipes_cpp
VPATH=$(NRROOT)/demo/src:$(NRROOT)/recipes:$(NRROOT)/utils:$(NRROOT)/demo/responses
CC=gcc
CFLAGS= -I$(NRROOT)/utils -I.
NRDIFF=$(NRROOT)/utils/nrdiff.pl
NRANS=$(NRROOT)/demo/answers
NRDAT=$(NRROOT)/demo/data
daniel@t400s:/usr/local/src/nr/recipes_cpp$ head -n20 makefile
.SUFFIXES:
.SUFFIXES: .reslt .res .out .o .cpp
# Set the path and parameters below to the correct values when needed
NRROOT= /usr/local/src/nr/recipes_cpp
VPATH=$(NRROOT)/demo/src:$(NRROOT)/recipes:$(NRROOT)/utils:$(NRROOT)/demo/responses
CC=gcc
CFLAGS= -I$(NRROOT)/utils -I.
NRDIFF=$(NRROOT)/utils/nrdiff.pl
NRANS=$(NRROOT)/demo/answers
NRDAT=$(NRROOT)/demo/data
NRLIBS=-lm
.cpp.o: ; $(CC) $(CFLAGS) -c $<
.o.out: ; $(CC) $(CFLAGS) -o $@ $^ $(NRLIBS)

Can anybody possibly identify the error or would you need further information?

Many thanks,

Daniel

davekw7x
06-14-2010, 10:49 AM
...makefile...
...
CC=gcc...

What happens if you make it

CC=g++


I am new to make

So, here's a suggestion: Compile a simple nr3 program with a command line first. Then incorporate whatever you think you need in the Makefile so that it gives the same command line and see how it goes. See Footnote.

If there are any problems, then show us the command and show us the file that you are compiling.


Regards,

Dave

Footnote: Start with a simple program and a simple Makefile. Build from there.

For example:

//
// Test program for nr3
//
// davekw7x
#include <nr3.h>

int main()
{
Int num = 10;
for (Int i = 2; i <= num; i++) {
VecDoub v(i+1);
for (Int j = 0; j < v.size(); j++) {
v[j] = j;
}

Doub sum = 0;
for (Int j = 0; j < v.size(); j++) {
sum += v[j];
}

cout << "The sum of the first " << setw(2) << i
<< " integers is " << sum
<< endl;
cout << "The square root of " << sum
<< " is " << sqrt(sum) << endl << endl;
}
return 0;
}


Here's a simple Makefile to start from:

#
# Simple makefile for a simple nr3 program
# davekw7x
#

NR3ROOT = /home/dave/nr3
CXX = g++
CXXFLAGS = -Wall -W

INCLUDES = -I$(NR3ROOT)/code
LDLIBS = -lm

TARGET = test_nr3

$(TARGET): $(TARGET).o
$(CXX) $(CXXFLAGS) $< $(LDLIBS) -o $@

$(TARGET).o: $(TARGET).cpp

%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $(INCLUDES) $< -o $@

clean:
rm -f $(TARGET) $(TARGET).o


Here's a run:

$ make clean;make
rm -f test_nr3 test_nr3.o
g++ -c -Wall -W -I/home/dave/nr3/code test_nr3.cpp -o test_nr3.o
g++ -Wall -W test_nr3.o -lm -o test_nr3

$ ./test_nr3
The sum of the first 2 integers is 3
The square root of 3 is 1.73205

The sum of the first 3 integers is 6
The square root of 6 is 2.44949

The sum of the first 4 integers is 10
The square root of 10 is 3.16228

The sum of the first 5 integers is 15
The square root of 15 is 3.87298
.
. Five more outputs
.


Note that tab characters are vital in Makefile (real tab characters in the rules, not spaces). For starters paste my Makefile into a test directory on your system, change the NR3ROOT definition, and then try it with the source file that I show.

fugu
06-15-2010, 02:23 PM
The g++ compiler did the trick eventually.

Many thanks also for the step by step intro with the makefile example.

Best regards,

Daniel

chiyangcheng
08-14-2014, 07:57 PM
Hi Dave,

I have read your answers about the command line options in compiling the C++ codes with g++. Could you explain what the option "-W" does in your examples? I cannot find it in the documentation.

Thank you in advance.

C. Y.
========================

What happens if you make it

CC=g++



So, here's a suggestion: Compile a simple nr3 program with a command line first. Then incorporate whatever you think you need in the Makefile so that it gives the same command line and see how it goes. See Footnote.

If there are any problems, then show us the command and show us the file that you are compiling.


Regards,

Dave

Footnote: Start with a simple program and a simple Makefile. Build from there.

For example:

//
// Test program for nr3
//
// davekw7x
#include <nr3.h>

int main()
{
Int num = 10;
for (Int i = 2; i <= num; i++) {
VecDoub v(i+1);
for (Int j = 0; j < v.size(); j++) {
v[j] = j;
}

Doub sum = 0;
for (Int j = 0; j < v.size(); j++) {
sum += v[j];
}

cout << "The sum of the first " << setw(2) << i
<< " integers is " << sum
<< endl;
cout << "The square root of " << sum
<< " is " << sqrt(sum) << endl << endl;
}
return 0;
}


Here's a simple Makefile to start from:

#
# Simple makefile for a simple nr3 program
# davekw7x
#

NR3ROOT = /home/dave/nr3
CXX = g++
CXXFLAGS = -Wall -W

INCLUDES = -I$(NR3ROOT)/code
LDLIBS = -lm

TARGET = test_nr3

$(TARGET): $(TARGET).o
$(CXX) $(CXXFLAGS) $< $(LDLIBS) -o $@

$(TARGET).o: $(TARGET).cpp

%.o: %.cpp
$(CXX) -c $(CXXFLAGS) $(INCLUDES) $< -o $@

clean:
rm -f $(TARGET) $(TARGET).o


Here's a run:

$ make clean;make
rm -f test_nr3 test_nr3.o
g++ -c -Wall -W -I/home/dave/nr3/code test_nr3.cpp -o test_nr3.o
g++ -Wall -W test_nr3.o -lm -o test_nr3

$ ./test_nr3
The sum of the first 2 integers is 3
The square root of 3 is 1.73205

The sum of the first 3 integers is 6
The square root of 6 is 2.44949

The sum of the first 4 integers is 10
The square root of 10 is 3.16228

The sum of the first 5 integers is 15
The square root of 15 is 3.87298
.
. Five more outputs
.


Note that tab characters are vital in Makefile (real tab characters in the rules, not spaces). For starters paste my Makefile into a test directory on your system, change the NR3ROOT definition, and then try it with the source file that I show.