Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

How to link two programs written in C++?

Status
Not open for further replies.

shedeed

Member level 4
Joined
Sep 26, 2004
Messages
70
Helped
2
Reputation
4
Reaction score
1
Trophy points
1,288
Location
Egypt
Activity points
631
i want to know how to link two programs written in C++
 

Re: linking in C++

what do you mean with linking?

Do you mean you want a compiler to make 1 binary out of the sources?
In that case, just combine the sources (use only 1 main ;-) ) and compile.
(What OS, what compiler are you using???)

Otherwise pipes might be a possible solution but what exactly do you want to do?

Antharax
 

Re: linking in C++

i mean that i have a program written that have a certain functions it does, and i want to use these functions in another program without cpying them again.
i've once read that #include<filename> or the "#define" preprocessor can be used
 

Re: linking in C++

shedeed said:
i mean that i have a program written that have a certain functions it does, and i want to use these functions in another program without cpying them again.

if your program is excutable it's no possiblity to 'link' with another one.
You can only write static or dynamic library that can be linked with other programs.

shedeed said:
i've once read that #include<filename> or the "#define" preprocessor can be used
- it's not true
 

Re: linking in C++

ah yes,
Now i see what you mean.

Just make a header file for the c/cpp file you want to reuse

Header file foo1.h
Code:
#ifndef foo1_h_
#define foo1_h_

int foo1(int argument);

#endif // foo1_h_

C-file foo1.c
Code:
#include "foo.h"

int foo(int argument)
{
  return argument;
}

Explanation of lines in header file:
line 1, 2 and 5 (#ifndef ..., #define ..., #endif ...) are the well know guard statements in header files to avoid multiple inclusions. When a header file is included in a c or cpp file (by using #include "headerYouWant.h") everything is just copied in the c file by the preprocessor. This is also true for header files included in other header files. In this way it is possible that a header file (eg myTypes.h which you use to make your own typedefs) is included multiple times in a c or c++ file. This is a problem for typedefs!

When you want to reuse the foo code in your main:

Code:
#include "foo.h"

int main(void)
{
  int result;

  result = foo(100);
}

Just add these to your project/makefile and let the compiler/linker do it's job

Antharax
 

    shedeed

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top