I have just started using GNU debugging tool and now I am stuck in some assignment. In this we are supposed to make three different .c files and then compile them into one program ( executable file ). My query is that is there any way we can switch .c files while debugging because the function call is in, say file1 and its definition is in file2. Can't I pass the file2 as soon as the gdb reaches that function call during analyzing code line by line.
Thank you.
is there any way we can switch .c files while debugging
You are probably looking for the GDB step command, which will automatically switch sources for you when you step into function defined in a different source file.
Related
My apologies if this is a simple question for some people but I can't find the solution anywhere.
I am an RStudio beginner and I want to call specific routines of an open source fortran77 simulation program (there is also c and c++ code in it) from within RStudio.
The Fortran program is using Makefiles for compiling and it generates many .o object files (by the way, I am using Unix). I wrote a wrapper file in fortran which compiles together with the simulation program and it is supposed to be used by RStudio for calling the fortran routines. I generate the shared object file .so of that wrapper file and all works well if I have simple calculations in that wrapper file. I am following the same process as in this excellent post:
http://www.r-bloggers.com/fortran-and-r-speed-things-up/
I use dyn.load and .Fortran successfully and I get results back as long as I do not call subroutines that are located in another file (and correspond to other object files and other .so files). When I try to call another subroutine from within the wrapper subroutine I get the following error:
Error in dyn.load("rwrapper.so") :
unable to load shared object '/home/adminuser/ESP-rSource/src/esrubps/rwrapper.so':
/home/adminuser/ESP-rSource/src/esrubps/rwrapper.so: undefined symbol: runit_
runit (without underscore) is another subroutine that is located in another file and has another object file. I then tried to make a second shared object file for that runit subroutine and I also load it with dyn.load but it did not fix the problem. I am probably doing something wrong here but I do not know what. Do I need to convert all object files to .so shared object files and then use dyn.load to load each one of them (there are around 100 of .o files) or would the "wrapper/communication" file approach work? Is there a way to establish communication between the fortran program and RStudio? I am pasting my RStudio script here for information only (note that the 2nd dyn.load does not make a difference):
myrwrapper <- function(rrrandom) {
if (!is.loaded('rwrapper')) {
dyn.load("rwrapper.so")
}
if (!is.loaded('esru_lib')) {
dyn.load("./home/adminuser/ESP-rSource/src/lib/esru_lib.so")
}
retvals <- .Fortran("RXCHNGE",icomp = as.integer(2), rCOUPLEVAR = as.numeric(rrrandom))
return(retvals$rCOUPLEVAR)
}
An easy solution would have been to write/read a text file from both programs and exchange data through that file, however my understanding is that this would make the simulations really slow because of the need to open/close a file at almost every time step, and so I am trying to avoid such an approach.
Thank you for your help.
This was actually a lot easier than I thought. It was a novice mistake with the shared .so file. Problem solved when creating the shared file and linking it to two (or more) object files rather than one. For example if you have 1.F which calls 2.F and the respective 1.o and 2.o, you should include both files when creating the shared file as:
gfortran -shared -o 1plus2.so 1.o 2.o
I was reading on Clang and Ch (c++ interpreters), but its not clear for me, is it possible to run a newly generated .cpp file without any installations? Because i need to run the final program on any pc...
ps. if yes, does anyone have a good example, where a .cpp file is being executed within c++ code?
This is probably impossible or at least very hard. You would have to include the whole compiler (including linker, assembler, optimizer, preprocessor, ...) inside your program and that would make it extremely big.
One way of doing this is with Clang (as you already noted), there is even a demo project called "Clang interpreter" in the source: http://llvm.org/viewvc/llvm-project/cfe/trunk/examples/clang-interpreter/
However I once tried to compile this "beast" into my program and gave up halfway, because the file size of the result binary (or binaries with external libraries) gets into tens of megabytes (maybe even a hundred).
My suggestion is to either produce a different script (e.g. bash/sh script, which you could execute on any unix machine) that can be interpreted easily.
As far as I know, it is impossible, because compilation process of a CPP file is like this-
Preprocessing: the preprocessor takes a C++ source code file and deals with the #includes, #defines and other preprocessor directives. The output of this step is a "pure" C++ file without pre-processor directives.
Compilation: the compiler takes the pre-processor's output and produces an object file from it.
Linking: the linker takes the object files produced by the compiler and produces either a library or an executable file.
So, there should be intermediate files and executable files.
More can be found here-
https://stackoverflow.com/a/6264256/7725220
Kind of depends on what you mean by "installations".
Yes you can distribute your program with a full compiler, compile the source code and then execute the final result (all from the original exe).
Please submit only one zip file .Your zip file should contain the following files:
ItemListClass.h
ItemListMethods.cpp
ItemListTests.h
ItemListTests.cpp
makefile
numbers.txt
How is it possible to turn in 2 cpp files? Does that mean I am gonna have 2 different projects and combine them into one? I understand classes, but I am unsure how they could be all used in a zip file.
Note: I am not looking for code; I am looking for understanding. Maybe someone has done something similar? this is a grocery list with integers instead of items.
C++ supports a concept called "separate compilation".
Basically, each .cpp file can be compiled independently from all the others, and the final program is made by "linking" all of the compiled files together.
ItemListClass.h
ItemListMethods.cpp
ItemListTests.h
ItemListTests.cpp
makefile
numbers.txt
This implies that ItemListClass.h provides the caller-visible interface for your ItemList, that the out-of-line implementation for the member functions of ItemList go in ItemListMethods.cpp, and that a test program (presumably with a main() function in ItemListTests.cpp) will exercise the ItemList functionality. I can see no particular reason to think that ItemListTests.h is useful... whatever ItemListTests could credibly contain is unlikely to be of use to any code other than ItemListTests.cpp, and if it was then it should really be moved into a "TestSupport.h" header or similar. But, the implication is that ItemListMethods.cpp should include ItemLists.h, and ItemListTests.cpp should include ItemListTests.h. numbers.txt is presumably input data that your ItemListTests.cpp will read through to populate an ItemList object during testing. The makefile should do something vaguely like:
ItemListTest: <tab> ItemList.o ItemListTest.h ItemListTest.cpp
<tab>g++ -g -o ItemListTest ItemList.o ItemListTest.cpp
ItemList.o: <tab> ItemList.h ItemList.cpp
<tab>g++ -g -c ItemList.cpp
You can then type "make" in the same directory to build an executable ItemListTest.
Each .cpp file implements a set of functions. The entire program is the union of those functions… the compiler (specifically the "linker", which is the last stage of the compiler) gathers the functions together and packages it into your executable.
A project usually contains numerous .cpp files. C++ has few rules about how the program is divided over them, but usually each contains one class or group of functions.
Header files exist so that each .cpp file can be aware of the functions defined in the others.
You should read about makefiles.
Basically, your makefile is the project you're submitting and is made up by four files.
A project in almost all C++ IDEs/compilers is allowed to have multiple source files. Typically you couldn't compile them from the zip file unless they are extracted. The reason they ask for a zip is because it is very easy to send/submit. It would have to be extracted after your instructor/examiner receives it.
I just started learning C++ with Dev C++ as my IDE. One of the tutorials I'm using has a page in it about compiling a program made up of multiple files. It's simple stuff at this point, I have one file with a function in it, and the other file has all the other required code to call the function and output the results. The problem is that the tutorial doesn't tell me how to join these files so I can compile the program and have it work. There's seems to be multiple ways of doing this and I'd like them all but I'm mainly look for the simplest one right now.
I should also mention that I'm new at this so please try and keep your explanations simple and understandable.
In general, you would add both .cpp files to your project under the same target. It IDE will automatically add both files to the build and link them together.
That said, Dev-C++ is very, very old and unmaintained. It has not seen updates in several years. I strongly urge you to use a different IDE. There are many to choose from, including a fork of Dev-C++ called wxDev-C++. I'd actually recommend Code::Blocks or Visual Studio Express, which are both much more modern and have better support for debugging and many other features.
I am not sure of Dev-C++, but the concepts remain the same. So, here is how you can try to get both the files to work together
Each C++ file is a compilation unit - meaning, the compiler will convert one .cpp / .cxx file to one .obj / .o file (on Windows and Linux (or any Unix)) respectively
The obj files, called the object files contain the machine code (am skipping few internal details here) for the classes and functions present in that particular file
If you want to access the functions present in a different compilation unit, you need to link those two object files
Linking is a term that is used to, well, link two object files
There is a separate process (other than the compiler) which does the linking of the object files
So,in your case, you need to use the dev-c++ compiler and create separate object files
Then using the linker you link both the object files to create the final executable
If there are functions that exist in the .cpp files that you want to reference, you use the header files. The header files contain the function/class declarations. The .cpp files will have the implementations. So, in one of your .cpp file, (say) A.cpp, you include the header B.hpp and use the functions in the B.hpp file. The inclusion of headers will tell the compiler that the function declarations exist elsewhere and that the linker will take care of stringing all these references together to create the final executable.
Hope this helps, else, please don't hesitate to mention the files you are using and I can suggest how to link both the .cpp files together.
You must include the other files by using the #include preprocessor directive
in the top of the file where you have the main() function
For example:
#include "filename.h"
...
/* rest of code containing main function goes here */
...
#include "path/filename.c"
main
{
...
...
...
}
I'm compiling own project. And it halted by this error:
LINK||fatal error LNK1181: cannot open
input file
'obj\win\release\src\lua\bindings.o'|
Compiling using Code::Blocks with VS 2005/2008 compiler under win7.
There are also lot of another empty directories where *.o files are missing.
What do they do?
A file ending in .o is an object file. The compiler creates an object file for each source file, before linking them together, into the final executable.
You've gotten some answers, and most of them are correct, but miss what (I think) is probably the point here.
My guess is that you have a makefile you're trying to use to create an executable. In case you're not familiar with them, makefiles list dependencies between files. For a really simple case, it might have something like:
myprogram.exe: myprogram.o
$(CC) -o myprogram.exe myprogram.o
myprogram.o: myprogram.cpp
$(CC) -c myprogram.cpp
The first line says that myprogram.exe depends on myprogram.o. The second line tells how to create myprogram.exe from myprogram.o. The third and fourth lines say myprogram.o depends on myprogram.cpp, and how to create myprogram.o from myprogram.cpp` respectively.
My guess is that in your case, you have a makefile like the one above that was created for gcc. The problem you're running into is that you're using it with MS VC instead of gcc. As it happens, MS VC uses ".obj" as the extension for its object files instead of ".o".
That means when make (or its equivalent built into the IDE in your case) tries to build the program, it looks at those lines to try to figure out how to build myprogram.exe. To do that, it sees that it needs to build myprogram.o, so it looks for the rule that tells it how to build myprogram.o. That says it should compile the .cpp file, so it does that.
Then things break down -- the VC++ compiler produces myprogram.obj instead of myprogram.o as the object file, so when it tries to go to the next step to produce myprogram.exe from myprogram.o, it finds that its attempt at creating myprogram.o simply failed. It did what the rule said to do, but that didn't produce myprogram.o as promised. It doesn't know what to do, so it quits and give you an error message.
The cure for that specific problem is probably pretty simple: edit the make file so all the object files have an extension of .obj instead of .o. There's room for a lot of question whether that will fix everything though -- that may be all you need, or it may simply lead to other (probably more difficult) problems.
A .o object file file (also .obj on Windows) contains compiled object code (that is, machine code produced by your C or C++ compiler), together with the names of the functions and other objects the file contains. Object files are processed by the linker to produce the final executable. If your build process has not produced these files, there is probably something wrong with your makefile/project files.
It is important to note that object files are assembled to binary code in a format that is relocatable. This is a form which allows the assembled code to be loaded anywhere into memory for use with other programs by a linker.
Instructions that refer to labels will not yet have an address assigned for these labels in the .o file.
These labels will be written as '0' and the assembler creates a relocation record for these unknown addresses. When the file is linked and output to an executable the unknown addresses are resolved and the program can be executed.
You can use the nm tool on an object file to list the symbols defined in a .o file.
Ink-Jet is right. More specifically, an .o (.obj) -- or object file is a single source file compiled in to machine code (I'm not sure if the "machine code" is the same or similar to an executable machine code). Ultimately, it's an intermediate between an executable program and plain-text source file.
The linker uses the o files to assemble the file executable.
Wikipedia may have more detailed information. I'm not sure how much info you'd like or need.