I want to import functions from another file in Microsoft Visual C++ 6.0. How can i do this? I have tried with this as follows:
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#import <functions.cpp>
where functions.cpp is the file name from where I want to import the functions. But this gives an error: F:\CC++\Term Project\Dos Plotter\Functiom Plotter.cpp(6) : fatal error C1083: Cannot open type library file: ‘Functions.cpp’: No such file or directory
How can I solve this problem?
The
#importdirective is used with type libraries, often COM or .Net, not C++ source files. For complete details, see the MSDN page.In order to include C++ functions from another file, you typically want to use the
#includedirective (details). This includes the code from the given file during compilation. Most often, you should include a header containing the function prototypes; it is possible to include code files, but not commonly needed or always safe.To do this, you should provide two files, a header and a source file, for your functions.
The header will read something like:
and the source:
To use this in another file, you do:
You should also note that a header should typically be included only once. The MSVC-standard method of guaranteeing this is to add
#pragma onceto the beginning.Edit: and to address the specific error you’ve posted, which applies to both
#importand#include, the file you’re attempting to include must be somewhere within the compiler’s search path. In Visual Studio, you should add the necessary path to the project includes (this varies by version, but is typically under project properties -> compiler).