Compiling Issues with Cygwin


Ashur
01-01-2015, 03:32 PM
I am using cygwin on my windows 7 to learn C++ and write some codes for my physics projects. I also have the NR book and code files to practice the coding and see how they work. However, I have problem compiling those codes under my cygwin OS which is supposed to be a Linux on top of my Windows OS. As long as I was practicing C codes from the book "C Plus Primer," I didn't have problem. What I was doing was that I was saving the source code as "sourcecode.c" in some directory of D:/cygwin64/home/user/nr3 in which there are also all the header files used in the codes under a separate subdirectory ../nr3/code. Then, I created .cpp version of the source code by "vi sourcecode.cpp". Then, I did "make sourcecode" and finally "./sourcecode" to compile the code. In most cases I didn't have problem. However, now that I am trying to do the same with NR3 codes, AFTER creating .cpp file, I am getting error message as follows as I type "make sourcecode" :
__________________
cc rational_function_interpolation.c -o rational_function_interpolation
rational_function_interpolation.c:1:25: fatal error: ../code/nr3.h: No such file or directory
#include <../code/nr3.h>
^
compilation terminated.
<builtin>: recipe for target 'rational_function_interpolation' failed
make: *** [rational_function_interpolation] Error 1
__________________
I have tried to follow all the suggestions in some posts of nr.com/forum but it seems that none is working for me. I think maybe cygwin is not capable of compiling those codes as written in NR book and code files. I would appreciate anyone's help in this regard. Since I am a new learner of C/C++, I don't assume that I am doing it correctly and it's cygwin incompatibility issue. But, just in need of some clarification.
I have attached the code I am trying to compile and run.

Sincerely
Ashur

bogwal
02-14-2015, 09:39 AM
There is no problem using Cygwin, the search path for your include is wrong. You could try defining it explicitly or just put the files you need in the same folder as your source. If the files are in the same folder just use e.g.

#include "nr3.h"

Your problem is not NR specific. It's no Cygwin compatibility problem.

bogwal
02-14-2015, 10:01 AM
I think I see the problem more clearly after reading your post again, you haven?t done any programming in C before. A Makefile is used when you have multiple files to compile and build instead of using long compiler input from e.g. the command line. Thus, if you haven't created one you shall not call "make". "./sourcecode" runs the compiled code, it doesn't compile it. To solve you problem (the easiest solution for you), have the header files in the same folder as your source

#include "../code/nr3.h" -> #include "nr3.h"

and the same for your other #include files. Then compile the code by

g++ -lm sourcecode.cpp -o sourcecode

The "-lm" might not be necessary depending upon your environment (it's to link the lib math) - including it however doesn't cause any problem. After successful compiling run the program by

./sourcecode

You should probably take a look at some book in basic C-programming. I hope this helps you (or any other beginner reading this).