-4

Given a main.cpp file :

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

#include "inoutfich.h"

using namespace std;

int main()
{
    /* chargement du texte en mémoire dans une string */
    char n_fichin[80];
    cout << "Nom du fichier à copier : " ;
    cin >> n_fichin;
    vector<string> texte;
    ChargeTexte(n_fichin, texte);

    // cherche ligne la plus longue
    unsigned iPLongue =  LigneLaPlusLongue(texte);
    cout << "La ligne la plus longue du texte est la ligne " << iPLongue;
    cout << "****  " << texte[iPLongue] << "    ****";

    return 0;

} // main

and a submodule inoutfich.cpp which works in an other programm.

a submodule header inoutfich.h such :

#ifndef _INOUTF_H
#define _INOUTF_H

#include <vector>
#include <string>

using namespace std;

void ChargeTexte(char [], vector<string>& );
void EcrireFichier(char [], const vector<string>&);

unsigned short LigneLaPlusLongue(const vector<string>&);

#endif

When I compile

`$g++ main.cpp -o out.o`

I get the following terminal error :

/tmp/ccunCT7N.o: In function `main':
main.cpp:(.text+0x5a): undefined reference to `ChargeTexte(char*, std::vector<std::string, std::allocator<std::string> >&)'
main.cpp:(.text+0x66): undefined reference to `LigneLaPlusLongue(std::vector<std::string, std::allocator<std::string> > const&)'

How to fix it ?

Hugolpz
  • 17,296
  • 26
  • 100
  • 187
  • Where are the definitions of your functions? You should link against libraries that contain definitions. – mic4ael May 12 '14 at 15:25
  • It's a linker issue... you need to link with the library or objects where those functions are defined. – jsantander May 12 '14 at 15:26
  • 1
    try `$g++ main.cpp inoutfich.cpp -o out.o` – Luke B. May 12 '14 at 15:26
  • @jsantander: "you need to link with the library or objects where those functions are defined", Is this the purpose of `#include "inoutfich.h"` – Hugolpz May 12 '14 at 15:26
  • You need to compile and link both main.cpp and inoutfich.cpp together. If inoutfich.cpp already included in a library file, link against the file. – Arun R May 12 '14 at 15:27
  • @LukeB. The `out.o` naming convention is normally reserved for object files, not executable binaries. – Konrad Rudolph May 12 '14 at 15:28
  • No, `#include "inoutfich.h" will get your code to know about the declarations of the functions at compile phase. Later the linker need to gather *all the definitions* for *all the symbols* used in your program to create the executable. – jsantander May 12 '14 at 15:28
  • @KonradRudolph Sorry, I don't actually use g++ in the command line, I just completed his example since he was missing the other cpp. – Luke B. May 12 '14 at 15:30
  • @jsantander `$g++ main.cpp inoutfich.cpp -o out.o` works ! – Hugolpz May 12 '14 at 15:31

1 Answers1

2

If you only do g++ main.cpp -o out.o , you will only be building main.cpp . To include the other .cpp file, do g++ main.cpp inoutfich.cpp -o out.o .

Peace.

ChemiCalChems
  • 612
  • 12
  • 31
  • As others mentioned `out.o` is at least a very unusual name for an executable! – πάντα ῥεῖ May 12 '14 at 15:58
  • True, out (without any extension) is the best for Linux, and if in Windows, he should use .exe.What I do is normally naming my executable as the main .cpp file (example.cpp) without the extension. So example, and thats it. – ChemiCalChems May 12 '14 at 16:02