[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 352
  • Last Modified:

#include <mycode>

#include <windows.h>


is useful.


i have lots of function i want to use, but i don't want to having to copy them all over my codes that use them,

how do I #include  to use them?

they are only functions like this:



#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

using namespace std;

void mousemove( HWND hwnd1, int a, int b  );

int main ()
{
   return 0;
}


void mousemove( HWND hwnd1, int a, int b  )
{
      POINT p;
      p.x = a;
      p.y = b;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );
      
}


0
Troudeloup
Asked:
Troudeloup
  • 3
  • 2
  • 2
  • +2
7 Solutions
 
Deepu AbrahamR & D Engineering ManagerCommented:
in MyCode.H add the following lines

#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

void mousemove( HWND hwnd1, int a, int b  );

and in your mycode.cpp file add the following lines

#include "mycode.h"

using namespace std;

void mousemove( HWND hwnd1, int a, int b  )
{
      POINT p;
      p.x = a;
      p.y = b;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );
     
}

in your main.cpp add following

#include "mycode.h"

int main ()
{
   mousemove( hwnd1, a, b  );
   return 0;
}

0
 
UrosVidojevicCommented:
1.)
Put declarations of all your functions in file "mycode.h".
For example:

#ifndef _MYCODE_H_
#define _MYCODE_H_

int myfunction1(int a);
double myfunction2(double b);

#endif

2.)
Put definitions of all your functions in file "mycode.cpp"
For example:

int myfunction1(int a) {
    return a*a;
}

double myfunction2(double b) {
    return b*b*b;
}

3.)
You need to include your header files in other files from which you want to use these functions:

#include "mycode.h"
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Troudeloup,

You probably want to copy them to their own source file and create a header file with the prototypes.

//-----------------------------
//  Myheader.h

#ifndef MYHEADER_H
#define MYHEADER_H

void mousemove (HWND hwnd, int a, int b);
//  other prototypes


#endif

You'll need to #include all of the header files that this file will need.
You'll #include "myheader.h" in any and/or every source in your program.

You MAY wish to copy the functions to another cpp file and compile it into an object file.  This file is made part of your project so that it is compiled once and references to the modules are satisfied at load time.



Good Luck,
Kent
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Deepu AbrahamR & D Engineering ManagerCommented:
you may have to add

using namespace std;
before

int main()
{..}
0
 
jkrCommented:
Separate them into several .cpp files and compile them together, e.g.


//mousemove.h
#include <iostream>
#include <windows.h>
#include <fstream>
#include <string>

void mousemove( HWND hwnd1, int a, int b  );

//mousemove.cpp
#include "mousemove.h"
using namespace std;
void mousemove( HWND hwnd1, int a, int b  )
{
      POINT p;
      p.x = a;
      p.y = b;
      ClientToScreen( hwnd1, &p );
      SetCursorPos( p.x, p.y );
     
}

//main.cpp
#include "mousemove.h"
using namespace std;

int main ()
{
   return 0;
}

Compile:

g++ -o main.exe main.cpp mousemove.cpp
0
 
TroudeloupAuthor Commented:
so I type

g++ -o main.exe main.cpp 1.cpp 2.cpp 3.cpp   // and so on to compile them?



#include "n.h"



I include this line in  n.cpp and main.cpp?

or every cpp involved?
0
 
TroudeloupAuthor Commented:
also, if I use window.h in more than one cpp, would more than one window.h get includes?

i don't want to increase the compiled file size unnecesarrily.
0
 
UrosVidojevicCommented:
You need to enter #include "n.h" in every file that uses its functions.
0
 
jkrCommented:
>>also, if I use window.h in more than one cpp, would more than one window.h
>>get includes?

Nothing to worry about, that does not increase the size of your code. Also, "windows.h" has so-called "header guards", so it will only be included once.
0
 
TroudeloupAuthor Commented:
#include "n.h"


and in n.cpp itself?



but i didn't create n.h,  does it mean the compiler would generate .h from .cpp involved automatically?
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now