Linking Code/Objects Using DevC++ IDE

Pages: 12
I posed this question 7 months ago on this forum with no luck and no luck searching the entire internet and asking friends who've programmed.

Simple Problem. Consider the code below:

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
int a;
cout<<"enter a number to find its double value: ";
cin>>a;
cout<<"The numbers double is: "<<doublevalue(a);

return (0);
}


That above code compiled alone will give an error saying the function doublevalue() is not defined, but I want to LINK the above function "doublevalue()" to another source code that defines it:

1
2
3
4
5
int doublevalue(int a){ //This is a function to be linked to the main code
int b;
b=2*a;
return (b);
}


Consider that I have now looked for 11 months (!!!) to this ultra simple task.
Many solutions included vague explanations but I need an exact step by step solution. I also do not need to know how useless DEV is, I dont care (Because I know there will be a flood of "dont use DEV its crap". I acknowledge its crap and outdated but I really need to know how to explicitly do this simple task in DEV.

Thanks
Last edited on
You need to declare the function in your main code/a header included from your main code:
 
int doublevalue(int a);
Define the function in a .h file and add it to the source code.
Hope it helps
closed account (3pj6b7Xj)
or include the header file .h that has the function into your main program .cpp file, that or explicity call it using the namespace if its IN ONE... nameofnamespace::doublevalue() etc...have any of us been helpfull to you ?
mrfaosfx wrote:
have any of us been helpfull to you ?


Unfortunately not :(

Its all too vague and it sounds like ive tried every single combination of the above. (countless times)

I need a step by step like this ( for example):

1) New project
2) New source file with the main code
3) include the following in that code: #include <doublevalue>
4) New source file with the second piece of code
5) Add: #include "doublevalue().h" in this source file at the top
6) save it as "doublevalue.h" in the directory of the dev c++ folder
7) compile this second source file
8) compile and run project

The above sort of step by step process is what I need.
Because im lazy or need to be spoonfed? Not at all. Rather, because the countless past tips have been all too ambigious and vague. This is why I need explicit steps.
Last edited on
1)You go to the general tab in the project options, and there is a button that says add/browse library or object.

Done.

Is there any specific reason that prevents you from using anything else but devc++?
No. He needs to declare the doublevalue function, that's the point.

Say, there's main.cpp
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
using namespace std;

int main(){
int a;
cout<<"enter a number to find its double value: ";
cin>>a;
cout<<"The numbers double is: "<<doublevalue(a);

return (0);
}


And doublevalue.cpp:
1
2
3
4
int doublevalue(int a){ //This is a function to be linked to the main code
b=2*a;
return (b);
}


He can compile a binary from doublevalue.cpp but none from main.cpp because, there's no declaration for the doublevalue function in there. The compiler doesn't know how to use this identifier, you have to tell him by adding this line to the main.cpp
 
int doublevalue(int a);


Now, if he succeds with the actual linking that will work, but people should usually put declarations of seperately compiled sources into a seperate header fille, so:
1. Create doublevalue.hpp (or what names you choose)
2. paste that int doublevalue(int a); line to it
3. include doublevalue.hpp from main.cpp:
 
#include "doublevalue.hpp" 

4. make sure that the directory containing doublevalue.hpp is one of the include search paths when compiling and that linker is set up to link in the object created out of doublevalue.cpp
Last edited on
Of course. But he asked how to link something.
Before you can link it in, it needs to be declared.
C allows implicit declarations while C++ does not. If you use a function, you must at least declare it before it is used by using a function prototype, which is often in a separate header file with a .h, .hh or .hpp file. If you don't declare it, you will get a compilation error, even if you try to link the source or object code that it is defined in (technically you don't even get to the linking stage, so it is incorrect to even say that you are trying to link it).

At the very least, if you don't want to create a header file for one function, a good practice when using the function in several files, declare it yourself before using it:
1
2
3
4
5
#include <iostream>
//other #includes

int doublevalue (int);
// any other code 
Last edited on
JoR wrote:
No. He needs to declare the doublevalue function, that's the point.

Say, there's main.cpp
1
2
3
4
5
6
7
8
9
10
11


#include <iostream>
using namespace std;

int main(){
int a;
cout<<"enter a number to find its double value: ";
cin>>a;
cout<<"The numbers double is: "<<doublevalue(a);

return (0);
}



And doublevalue.cpp:
1
2
3
4


int doublevalue(int a){ //This is a function to be linked to the main code
b=2*a;
return (b);
}



He can compile a binary from doublevalue.cpp but none from main.cpp because, there's no declaration for the doublevalue function in there. The compiler doesn't know how to use this identifier, you have to tell him by adding this line to the main.cpp


int doublevalue(int a);



Now, if he succeds with the actual linking that will work, but people should usually put declarations of seperately compiled sources into a seperate header fille, so:
1. Create doublevalue.hpp (or what names you choose)
2. paste that int doublevalue(int a); line to it
3. include doublevalue.hpp from main.cpp:


#include "doublevalue.hpp"


4. make sure that the directory containing doublevalue.hpp is one of the include search paths when compiling and that linker is set up to link in the object created out of doublevalue.cpp


Before I can do any of that, I have to compile the doublvalue source code right?
But I cannot even do this!!
It says: "[Build Error] [Project1.exe] Error 1"
NOTE: I pasted the following:

1
2
3
4
int doublevalue(int a){ //This is a function to be linked to the main code
b=2*a;
return (b);
}


Then I saved it as a "*.h, *.hpp, *.rh, *hh.
And compiled and it gave that error quoted above.

I also tried compiling it after saving it as normal source code but it gives another error:
"[linker error]undefined reference to 'winmain@16'
Id returned 1 exit status'
Last edited on
I don't know anything about dev-cpp but it should work as long as you only try to compile an object file, not link an excecutable with it.
well I cannot even do the first step: compile the doublevalue source code. pathetic.
I guess you're trying to compile AND link it to an excecutable in one step...
What error message do you get?
I posted the error message in my above post. I tried compiling with normal source file and h file (see above)

I cannot even compile a 4 line piece of code. Surely the solution is easy, no?
Last edited on
Wow dude, sorry I didn't see this post earilier, linker errors can be tough. In wxDev-C++ (which I remeber is simular enough to Dev-C++) you have to include the .h in your project by clicking Project -> Add to Project then navigate to the .h you made, this tells the makefile to include the header file in your project. Without this you are referencing a file in your code that the compiler doesn't know to build.

You don't need to compile the .h seperatly, infact I don't even know how you would go about doing that. It gets compiled with your project as a whole. I'll keep a close eye on this thread until we solve this issue. You can PM me but I don't always notice them right away.
Last edited on
You need 3 files:

1) A cpp file to have your main() function
2) A cpp file to have your doublevalue() function
3) A header file to have your doublevalue prototype

I will call these "main.cpp", "double.cpp" and "double.h"

Here:

1
2
3
// double.h

int doublevalue(int a);

1
2
3
4
5
6
7
8
// double.cpp

#include "double.h"

int doublevalue(int a)
{
  return a*2;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// main.cpp

#include <iostream>
#include "double.h"
using namespace std;

int main()
{
  int a;
  cout<<"enter a number to find its double value: ";
  cin>>a;
  cout<<"The numbers double is: "<<doublevalue(a);

  return (0);
}



Now depending on what IDE you're using, making these 3 files work togehter can vary.

Generally, you have a "project" and the IDE will build/compile/link all files that are part of that project.

So look around in your IDE for a "add file to project" option or something that sounds similar and make sure all 3 files are part of your project. Then Build normally.

I'm not familiar with DevCPP (and you shouldn't be either because it's ancient, I recommend picking up something more up to date). But it's probably set up the same way.
Last edited on
Also, with Dev-C++ I remeber a bug or something where sometimes when you would add a file to the project after compiling it you would recieve an error for no apparent reason, it would either reference a '*.o' object where '*' was the name of the file you tried to add or a 'Makefile.win' error, I can't remeber which. The solution was to blow away everything except the .dev, .cpp and .h files then relink and recompile the whole thing.

EDIT: Notice the DOUBLE QUOTES that Disch used instead of the greater then less then brackets. This tells your compiler to look for the header file in the directory you are currently working in. So if you had your main.cpp file in "C:\Code" then double.h would HAVE TO BE in "C:\Code" as well, the same goes for double.cpp. If you had double.h in "C:\Code\double" then you would have to use:
 
#include "C:/double/double.h" 

for your compiler to find it. There are ways around this by using the includes directory under project options but we'll save that for after we get this problem solved.
Last edited on
ComputerGeek and Disch. I am grateful for trying to help me solve this. Remember that even if I wasnt using Devcpp this info is still needed to introduce me on how to build seperately!

Anyway. What I did was:

opened new project
included 3 source files with each one as Disch quoted
Saved two of them as cpp source files option
saved the other one as a .h file option.

Then I compile and run and the following error comes up:


double.h: no such file or directory
in function 'int main()':
`doublevalue' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
E:\CPP\Dev-Cpp\Makefile.win [Build Error] [Untitled1.o] Error 1


This obviously comes down to telling the compiler to include it all together. Yet when I look at project options it quotes:

3 files [2 sources, 1 headers, 0 recources


so it does indeed recognise the files I saved.

I think as you guys implicitly suggest; there is one more step to complete it.
I know that it's not what you want to hear but I was using Dev C++ and was getting all sorts of errors that I wasn't getting in any other IDE. Just take that for what it's worth.
Pages: 12