Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Importing Functions from a different C file

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?

like image 993
Rafi Kamal Avatar asked Sep 02 '25 11:09

Rafi Kamal


2 Answers

The #import directive 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 #include directive (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:

#pragma once

void Function(int arg);

and the source:

#include "functions.hpp"

void Function(int arg) { ++arg; }

To use this in another file, you do:

#include "functions.hpp"

void OtherFunction()
{
    Function(2);
}

You should also note that a header should typically be included only once. The MSVC-standard method of guaranteeing this is to add #pragma once to the beginning.

Edit: and to address the specific error you've posted, which applies to both #import and #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).

like image 188
ssube Avatar answered Sep 03 '25 23:09

ssube


1) Did you mean functions.hpp? C/cpp files should not be #included unless you know very well what you're doing.

2) Add the location of the file to the custom include path in the project properties, or use the include "foo" format instead of include <foo>

3) Import is undefined in C. You need to separate prototypes and implementations, include-guard the prototypes file, and #include the prototypes file.

like image 36
moshbear Avatar answered Sep 04 '25 01:09

moshbear