Error undefined reference to std

3
#include <iostream>
using namespace std;
int main()
{
  int nombre, carre ;
  cout << "Introduza un numero : " ;
  cin >> nombre ;
  carre = nombre * nombre ;
  cout << "A raiz quadrada est ; " << carre ;
}

When compiling with GCC:

  

gcc -Wall -Wextra -Werror -o calc_carre calc_carre.cpp

I get:

  

calc_carre.cpp: (. text + 0xe): undefined reference to std::cout' calc_carre.cpp:(.text+0x13): undefined reference to std :: basic_ostream> & std :: operator <    & quot; (std :: basic_ostream > & amp ;, char const *) 'calc_carre.cpp: (. text + 0x1f):   undefined reference to std::cin' calc_carre.cpp:(.text+0x24): undefined reference to std :: istream :: operator > (int &) '   calc_carre.cpp: (. text + 0x3a): undefined reference to std::cout' calc_carre.cpp:(.text+0x3f): undefined reference to std :: basic_ostream> & std :: operator <    > (std :: basic_ostream > & amp ;, char const *) 'calc_carre.cpp: (. text + 0x4f):   undefined reference to std::ostream::operator<<(int)' /tmp/cclJ9vPR.o: In function __ static_initialization_and_destruction_0 (int, int) ':   calc_carre.cpp: (. text + 0x7d): undefined reference to    std::ios_base::Init::Init()' calc_carre.cpp:(.text+0x8c): undefined reference to std :: ios_base :: Init :: ~ Init () 'collect2: error: ld   returned 1 exit status

No C:

#include <stdio.h>
int main()
{
  int nombre, carre ;
  printf ("Introduza un numero") ;
  scanf ("%d", &nombre) ;
  carre = nombre * nombre ;
  printf (" A sua raiz quadrada é: %d, nombre) ;
}

When I compile, I do not get any error, but see through your eyes:

    
asked by anonymous 24.02.2017 / 11:49

1 answer

5

If you are using C ++ compile with g++ :

g++ -Wall -Wextra -Werror -o calc_carre calc_carre.cpp

If you want to insists on gcc , which I do not recommend, add the default library:

gcc -lstdc++ -Wall -Wextra -Werror -o calc_carre calc_carre.cpp

In C you have an error too:

#include <stdio.h>
int main() {
    int nombre, carre ;
    printf("Introduza un numero");
    scanf("%d", &nombre);
    carre = nombre * nombre;
    printf("A sua raiz quadrada é: %d", carre);
}

See running on ideone . And at Coding Ground . Also put it on GitHub for future reference .

This code is obviously not calculating the square root, but it's just math.

    
24.02.2017 / 12:12