2

I need to learn to code in C for my studies, so i use Visual studio code for this. I had to code in Pascal before, that's why I already have xcode, install fpc.

Recently, i wanted to do some cleaning on my storage and i deleted few applications. Unfortunately, i delete Xcode (not on purpose). Now that i need it to code in C, i re-installed it. I can't compile my program. I have an issues with files i think. i got the clang error (clang: error: no input files) and got this in visual studio code:

Undefined symbols for architecture x86_64:
  "_a", referenced from:
      _voiturePresente in q2-32f9ff.o
     (maybe you meant: _modiff_adresse_client, _adresse )
  "_i", referenced from:
      _saisieClient in q2-32f9ff.o
      _voiturePresente in q2-32f9ff.o
      _VoituresRepares in q2-32f9ff.o
     (maybe you meant: _infoClient, _immatriculation )
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

My code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

extern int i,a;
    char nom[5][50]; 
    char prenom[5][50];
    char adresse[5][1000];
    char marque[5][50]; 
    char modele[5][50];
    int immatriculation[5];
    int statut_reparation[5]; //un booléen est defenis comme valant soit 0 soit 1, n'ayant pas de type pour ca, j'ai mis int


void saisieClient() {
    for (i=0; i<5; i++) {
        printf(" nom du client %d \n", i);
        scanf("%s", nom[i]);
        printf(" Prénom : \n");
        scanf("%s", prenom[i]);
        printf(" adresse : \n");
        scanf("%s", adresse[i]);
        printf(" marque voiture : \n");
        scanf("%s", marque[i]);
        printf(" modèle : \n");
        scanf("%s", modele[i]);
        printf(" immatriculation : \n");
        scanf("%d", &immatriculation[i]);
        printf(" statut des réparation : \n");
        scanf("%d", &statut_reparation[i]);
    }
}
void infoClient(int a) {
    printf("nom : %s \n", nom[a]);
    printf("prenom : %s \n", prenom[a]);
    printf("adresse : %s \n", adresse[a]);
    printf("marque voiture: %s \n",marque[a]);
    printf("modele voiture: %s \n",modele[a]);
    printf("immatriculation voiture: %d \n",immatriculation[a]);
    if (statut_reparation[a]==1) 
    {
        printf("statut des reparations: réparé \n");
    }
    else 
    {
        printf("statut des reparations: pas réparé \n");
    }
}
void voiturePresente() {
    for (i=0 ; i<5 ; i++){
        printf("marque : %s \n", marque[i]);
        printf("modele : %s \n",modele[i]);
        printf("immatriculation : %d \n",immatriculation[i]);
        if (statut_reparation[a]==1) 
        {
            printf("statut des reparations: réparé \n");
        }
        else 
        {
            printf("statut des reparations: pas réparé \n");
        }
    }
}
void modiff_adresse_client(int x){
        printf("nouvelle adresse : ");
        scanf("%s", adresse[x]);
}
void VoituresRepares(void) {
    for (i=0; i<5; ++i) 
    {
        if (statut_reparation[i]==1) 
        {
            printf("la voiture du client %d \n", i);
            printf("est réparé. voici sa fiche client: \n");
            printf("nom : %s \n", nom[i]);
            printf("prenom : %s \n",prenom[i]);
            printf("adresse : %s \n", adresse[i]);
            printf("marque voiture: %s \n",marque[i]);
            printf("modele voiture: %s \n",modele[i]);
            printf("immatriculation voiture: %d \n",immatriculation[i]);
        }
    }
}


int main () {
    saisieClient();
    infoClient(3);
    voiturePresente();
    modiff_adresse_client(3);
    VoituresRepares();
}

I uninstalled both (Xcode and Visual Studio Code) but still have the same problem. I also tried again few command like xcode-select --install, but nothing's changed.

PS: i have the latest version of OS, Visual studio code and Xcode. And i got this when i do gcc -v:

Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin18.2.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
James Z
  • 12,209
  • 10
  • 24
  • 44
A.Patrick
  • 31
  • 7

1 Answers1

1

The line

extern int i,a;

tells the compiler (and the linker) that the symbols i and a are defined in another translation unit.

If they're not defined somewhere then you get this kind of error. The primary solution to your problem is to not only declare the variables, but to define them. Just like you do with all your other variables. You do that by removing the extern keyword.

The proper solution is not to define them as global variables, but only as local variables inside the function where the variables are used.

My personal reflection of the code you present tells me that you need to take a few steps back, go back to your books, tutorials or class notes. Perhaps even to the beginning.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Okay, thank you so much, it works. I spend 3 hours on it ahah. Yeah i know my code must not be that great, i start using C yesterday to start this exercice, and we have no books, no courses, nothing to learn it. So i did on myself ahah, just trying to understand how it works for now ahah. Thanks for the help ^^ – A.Patrick Feb 22 '19 at 22:00