Connecting Tech Pros Worldwide Forums | Help | Site Map

Include custom libraries

compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#1: Jun 17 '07
Hello, and thank you for viewing this post.
I was just wondering how to make includes for programs (includes like iostream.h, except custom)

I have a few functions that I would like to have in my programs with out adding on a few thousand lines of code, so any help would be great. The only problem is (other then having to idea on how to make includes) is that the functions that I want to make into an includable file need incliudes to run, so, how would I do this??

Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#2: Jun 17 '07

re: Include custom libraries


Quote:

Originally Posted by compman9902

Hello, and thank you for viewing this post.
I was just wondering how to make includes for programs (includes like iostream.h, except custom)

I have a few functions that I would like to have in my programs with out adding on a few thousand lines of code, so any help would be great. The only problem is (other then having to idea on how to make includes) is that the functions that I want to make into an includable file need incliudes to run, so, how would I do this??

Create a *.h file containing your functions with other includes.

After that just add your won custom include using "name.h" instead of <name.h>

Savage
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#3: Jun 17 '07

re: Include custom libraries


Quote:

Originally Posted by Savage

Create a *.h file containing your functions with other includes.

After that just add your won custom include using "name.h" instead of <name.h>

Savage

How will the function know what the name of the new variable is?
such as this:
Expand|Select|Wrap|Line Numbers
  1. exampleFunction(string)
  2. {
  3. string = otherExample;
  4. }
  5.  
will that work for displaying the string that the user sends to the function?
Savage's Avatar
Expert
 
Join Date: Feb 2007
Posts: 1,737
#4: Jun 17 '07

re: Include custom libraries


Quote:

Originally Posted by compman9902

How will the function know what the name of the new variable is?
such as this:

Expand|Select|Wrap|Line Numbers
  1. exampleFunction(string)
  2. {
  3. string = otherExample;
  4. }
  5.  
will that work for displaying the string that the user sends to the function?

I'm not sure that I understand your question.

Can you reform your question?

Savage
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#5: Jun 17 '07

re: Include custom libraries


Quote:

Originally Posted by Savage

I'm not sure that I understand your question.

Can you reform your question?

Savage

Sure.
When you make a function, a certain variable is usually declared for the variable that the program is passing to the function. Basically, How would the function in the include be able to read the variable passed to it by the program? Please give me code examples. Thank you.
JosAH's Avatar
Expert
 
Join Date: Mar 2007
Posts: 10,611
#6: Jun 17 '07

re: Include custom libraries


Quote:

Originally Posted by compman9902

Sure.
When you make a function, a certain variable is usually declared for the variable that the program is passing to the function. Basically, How would the function in the include be able to read the variable passed to it by the program? Please give me code examples. Thank you.

You're overcomplicating matters; suppose I have two great functions, foo and bar:

Expand|Select|Wrap|Line Numbers
  1. int foo(double d) { /* great stuff goes on in here ... */ }
  2. int bar(long l) { /* even greater stuff goes on in here */ }
  3.  
I can build a header file foobar.h for those great functions like this:

Expand|Select|Wrap|Line Numbers
  1. #ifndef FOO_BAR
  2. #define FOO_BAR
  3. extern int foo(double);
  4. extern int bar(long);
  5. #endif
  6.  
and all I have to do is to include that header file in other source files:

Expand|Select|Wrap|Line Numbers
  1. #include "foobar.h"
  2.  
  3. int main() {
  4.    int r1= foo(3.1415);
  5.    int r2= bar(12345678L);
  6.    /* other stuff here ... */
  7.    return 0;
  8. }
  9.  
When it comes to linking, the functions foo and bar should be available of course,
maybe in a library or as a compiled translation unit of their own. The compiler
just assumes function foo and bar exist somewhere because I declared them so
in my include file foobar.h. Note the little tag at the top of the include file that
protects the content of the file to be included more than once.

kind regards,

Jos
compman9902's Avatar
Member
 
Join Date: Mar 2007
Location: Anchorage, Alaska
Posts: 105
#7: Jun 17 '07

re: Include custom libraries


Quote:

Originally Posted by JosAH

You're overcomplicating matters; suppose I have two great functions, foo and bar:

Expand|Select|Wrap|Line Numbers
  1. int foo(double d) { /* great stuff goes on in here ... */ }
  2. int bar(long l) { /* even greater stuff goes on in here */ }
  3.  
I can build a header file foobar.h for those great functions like this:

Expand|Select|Wrap|Line Numbers
  1. #ifndef FOO_BAR
  2. #define FOO_BAR
  3. extern int foo(double);
  4. extern int bar(long);
  5. #endif
  6.  
and all I have to do is to include that header file in other source files:

Expand|Select|Wrap|Line Numbers
  1. #include "foobar.h"
  2.  
  3. int main() {
  4.    int r1= foo(3.1415);
  5.    int r2= bar(12345678L);
  6.    /* other stuff here ... */
  7.    return 0;
  8. }
  9.  
When it comes to linking, the functions foo and bar should be available of course,
maybe in a library or as a compiled translation unit of their own. The compiler
just assumes function foo and bar exist somewhere because I declared them so
in my include file foobar.h. Note the little tag at the top of the include file that
protects the content of the file to be included more than once.

kind regards,

Jos

thanks, just finished it
Reply


Similar C / C++ bytes