-1

I'm trying to read a struct of a program i wrote in c++ into a c program. I created a .h with the struct and the extern C for the function. Then i wrote the C++ program that read a csv file and save some elements in a struct. Now i have to recall the struct in a C program and read the values but i'm having a lot of problems. I don't know if i make some mistake in the .cpp file , in the .h file or in the C program. I'm working under linux.

My .h file is :

typedef struct Buses{
 int maggiore;
 int minore;
}BusesStruct;

#ifdef __cplusplus
extern "C" {
#endif

    void get_buses(BusesStruct *);

#ifdef __cplusplus
}
#endif

My cpp is

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <algorithm>
#include "lettura.h"
using namespace std;

void get_buses(BusesStruct * bus_scelti){
//leggo il file csv
    string line,nomefile;
    ifstream myfile;
    cout << "Quale file si vuole aprire? " << endl;
    cin >> nomefile;
    myfile.open(nomefile.c_str());
    int j=0;
    if (myfile.is_open()) {
        int primobus;
        int secondobus;
        cout << "Digitare il numero di uno dei due BUS sul quale si vuole eseguire l'operazione (1-9): " << endl;
        cin >> primobus;
        cout << "Digitare il numero di un secondo BUS sul quale si vuole eseguire l'operazione (1-9): " << endl;
        cin >> secondobus;
        int bus1_scelto;
        int bus2_scelto;
        while (getline(myfile,line)) {
            istringstream myline(line);
            string bus[10];
            for (int i=0; i<9; i++){
                    getline(myline,bus[i+1],';');
                }

            for (int i=0; i<10; i++){
                if(i == primobus){
                    int bus1_scelto_c = atoi(bus[i].c_str());
                    bus1_scelto=bus1_scelto_c;
                }
                if(i == secondobus){
                    int bus2_scelto_c = atoi(bus[i].c_str());
                    bus2_scelto=bus2_scelto_c;
                }
            }
            bus_scelti[j].maggiore=max(bus1_scelto,bus2_scelto);
            bus_scelti[j].minore=min(bus1_scelto,bus2_scelto);
            j=j+1;          
        }   
    }
}

In my C file i wrote #include lettura.h to recall the function :

BusesStruct bus_scelti[10005];
get_buses(bus_scelti);

And to define my vector and recall the function. I got the error :

reference not define to 'get_buses'.

Can you help me? Thanks

Iceknight
  • 1
  • 6

1 Answers1

1

According to your comment this is how you're compiling:

g++ -c lettura.cpp for the cpp file. For the c file i use g++ complete.c -o complete

This is compiling the C++ file into an object file and then compiling the C file into an executable. You need to link in the object file as well.

A few options:

g++ -c lettura.cpp                   # compile C++ file to lettura.o
gcc -c complete.c                    # compile C file to complete.o
g++ lettura.o complete.o -o complete # link object files into executable 'complete'

g++ lettura.cpp complete.c -o complete # compile and link in one step

Note that I'm using gcc to compile the C code and g++ to compile the C++ code and to link.

Ideally you should use a Makefile for this, but I think that's beyond the scope of this question/answer.

Kevin
  • 6,993
  • 1
  • 15
  • 24