0

I'm new to this topic, so i started with basic program of printing 10 numbers. The function has been defined in C++ and declared in my customized header file, now i want to access this function in C file. I have created the files, but i'm facing fatal error: iostream: No such file or directory
Please let me know where i'm going wrong. I have attached the programs below:
C++ code:

#include <iostream>
#include "my_header.h"

// Function definition
void printNumbers() {
    for (int i = 1; i <= 10; i++) {
        std::cout << i << " ";
    }
    std::cout << std::endl;
    int main()
    {
        return 0;
    }
}

Header file:

#include "my_cpp.cpp"

#ifndef NUMBERS_H
#define NUMBERS_H

// Function declaration
void printNumbers();

#endif

C code:

#include <stdio.h>
#include "my_header.h"
int main() {
    // Call the function defined in C++
    printNumbers();
    return 0;
}

Thanks in advance.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
  • 1
    Check out the following question: https://stackoverflow.com/questions/2744181/how-to-call-c-function-from-c – ag00se Jul 10 '23 at 06:16
  • 2
    Basically: do not call C++ from C. Rewrite your `main()` in C++ and stick to C++ throughout. (The other way around, calling C from C++, is kinda feasible) – pmg Jul 10 '23 at 06:17
  • 8
    Don't `#include` source files. – Some programmer dude Jul 10 '23 at 06:18
  • 3
    And why have you tried to nest `main` *inside* the `printNumbers` function? What resources are you using to learn programming and C++? They don't seem to do a very good job. Please invest in [some good C++ books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Even if they are expensive, they will pay off in the long run. – Some programmer dude Jul 10 '23 at 06:20
  • @pmg What's the main problem with calling functions written in C++ from C? As long as they have C linkage, it should be fine. – Ted Lyngmo Jul 10 '23 at 06:35
  • @TedLyngmo: for example: the internals of a C++ function with C linkage can use exceptions which C has no way to deal with. ... I think ... I don't really know C++, much less how to integrate it with C. – pmg Jul 10 '23 at 06:45
  • @pmg The C API provided by a C++ library must make sure to not throw exceptions for sure. I've provided C APIs for C++ libraries a few times, and it's usually not a big problem. – Ted Lyngmo Jul 10 '23 at 06:50
  • Ok, but his C++ `printNumbers()` function uses `std::cout`. If standard output is redirected to a network file... and the network goes down mid write... there comes an exception – pmg Jul 10 '23 at 07:12
  • @pmg i want to call the function from C++ to C, is there any way other then linking both the programs. – Basavaraj Kittali Jul 10 '23 at 09:36
  • @Someprogrammerdude thanks for the link, I will go through the books. – Basavaraj Kittali Jul 10 '23 at 09:38

1 Answers1

3

The mistake here is that you have included a source file in a header file

#include "my_cpp.cpp"

The results in your C compiler seeing your C++ code and hence you get the error you get.

C and C++ operate on a model of separate compilation, which that each C or C++ source file must be compiled independently. A source file and the header files it includes is called a translation unit.

How you compile each source file separately and then link them together depends on what compiler/IDE you are using, but using #include is not the way to do this.

Also you have other problems to do with mixing C and C++. It's not clear why you are doing this, it's a strange thing for a newbie programmer to be doing. But if you really want to you need to make the following changes. These changes are to give your C++ function C linkage, which is necessary to call a C++ function from C.

Change your header file like this

#ifndef NUMBERS_H
#define NUMBERS_H

#ifdef __cplusplus
extern "C" {
#endif

// Function declaration
void printNumbers();

#ifdef __cplusplus
}
#endif

#endif

The extern "C" part gives your function C linkage, but this code is only legal in C++. Therefore it has to be wrapped in #ifdef __cplusplus to make sure that this is only seen by the C++ compiler.

Additionally you can define your function with the same linkage in your C++ source file

// Function definition
extern "C" void printNumbers() {

though this is not really necessary, the declaration is sufficient as long as you follow best practise and include the header file in the source file.

john
  • 85,011
  • 4
  • 57
  • 81
  • Minor nitpick: Defining `printNumbers` with C linkage isn't needed since it's already declared to have C linkage. – Ted Lyngmo Jul 10 '23 at 06:55
  • 1
    @TedLyngmo Thanks for the clarification. I wasn't completely certain (hence why I said should) but I put it there to be on the safe side. – john Jul 10 '23 at 06:57