Linux - GeneralThis Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I am new to this forum and to gcc/g++ and I need to know exactly how to declare a function in g++. I wrote a C++ program to calculate the GCD of two numbers, I've tested it and it works. I wanted to compile the program and target a mips processor so I issued the following command
The program compiled fine, but would link because the linker for some reason did not reconize the iostream header file, no idea why?
So I used the -c option and like I said it compiled fine. However when I look over the disassembled list the assembly code is not quite correct, it looks as if the compiler placed the function "GCD" before the main() and before any initialization period. I read over the first few instructions and sure enough I was right because the function is placed before any initialization the program starts off within the function and can't get out its in an infinite loop and can't get out. I figured it must have something to do with how I am declaring the function and I wanted to ask you guys.
Someone help me please. The C++ code is pasted in below as well as the disassemble list.
In your case, have a header file of the same name as your source and place your prototype there. It's a good practise to get into as by default, the C compiler is giving you the default definition of any given function, something like int function();
Do this:
Code:
#ifndef _YOURFILENAME_H
#define _YOURFILENAME_H
unsigned int GCD_FUNCT( unsigned int, unsigned int );
#endif
And include that header where ever you use your function.
Code:
#include "iostream"
#include "iomanip"
#include "math.h"
#include "yourfilename.h"
using namespace std;
int main() {
unsigned int ARRAY[32], GCD[16];
unsigned int Loop_Control_V, Reg1, Reg2, new_value;
But do check out the tutorial, C & C++ will give you the firepower you need but hitting the ground running (like your doing) can cost you in learning curves. And it's a really good idea to place functions into their own source files if your project is going to grow any size at all. Or (at the very least) in this case, place you entire function before your main function. Your better off separating them into separate files in the long run!
I did what you said and I defined another file and I put the function in that file and put the #include GCD_FUNCT.h statement in my code. However still when I compile the disassemble list looks the same, for some reason the function is being placed before initilization and the main which can not be correct. I've pasted in my files in below so you can take look. Thanks again.
Consider asking the moderator to move this thread from "General" to "Programming." The "General" forum is for Linux questions not addressed in other fora.
This compiles just fine under Slackware's KDevelop IDE environment for C++ (G++):
Code:
/***************************************************************************
* Copyright (C) 2007 by derrick_chi, perry
***************************************************************************/
#ifndef _GCD_FUNCT_H
#define _GCD_FUNCT_H
unsigned int GCD_FUNCT( unsigned int REGa, unsigned int REGb);
#endif
I want to thank you for taking the time to help me out and show me my errors in programming standards. However lets say I simplify the situation and get rid of the function and the header all together as I have done in the code below.
Now the code above compiles fine and I can run it and get the results I want ( add a cout statement to it and see for yourself). The problem is that when I try do a sde-g++ compile or a mips-elf-g++ compile the machine code I get out isn't correct. I have pasted in the new disassemble list below for further explanation, ( jump past the section of code for the explanation of the problem).
OK here is the problem there is something wrong with the way the compiler is compiling the code because it is creating branches that I believe go to the wrong addresses. Lets look at the section of code below.
This section of code is basically the first loop in the orginal C++ program where Array[]'s values are being calculated and placed into the array, in the case of the processor the values should be placed on the stack, and the stack pointer should be incremented.
OK the code on lines 0-1c is setting up the stack pointer. The code in lines 10 - 20 are calculating the values for Array.
(new_value = i + ( (3 * i) + 7 ),
OK the code on line 20 is storing the calculated value onto the stack at a2(which holds the pointer to the stack) + 0, and once the value is stored the code checks to see if 31 "new_values" have been stored and if not it branches back to line 10 and begins to calculate the next "new_value".
NOW the problems is that the location of the stack pointer is never incremented, or decremented anywhere in this section of code, as a matter of fact the machine code ( acc20000 sw v0,0(a2) )
points that out clearly, its adding 0 to the location of the stack and therefore the new_values are over written by the next value being calculated and added to the stack "at the very same spot" that the last values were added. Do you see that?
I don't know how the compiler generated this type of machine code because I can't work so there must be something that I am doing wrong, any help you can provide I do greatly apprieciate, and you help so far has been great! Fill free to post this where ever you like.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.