[+] Kompilacja progamów c++ w konsoli

Potrzebujesz pomocy z C, C++, perl, python, itp.
Never

[+] Kompilacja progamów c++ w konsoli

Post autor: Never »

Witam.
Kiedy próbuję uruchomić przykładowy program napisany w c++ w konsoli Linuksa:

Kod: Zaznacz cały

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
cout << "123";

int q;
cin >> q;
return 0; 
}


Po uruchomieniu programu za pomocą polecenia:

Kod: Zaznacz cały

gcc program.cpp
otrzymuję następujący błąd:

Kod: Zaznacz cały

/tmp/ccPSfIUf.o: In function `main':
main.cpp :( .text+0x14): undefined reference to `std::cout'
main.cpp :( .text+0x19): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std: :o perator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
main.cpp :( .text+0x28): undefined reference to `std::cin'
main.cpp :( .text+0x2d): undefined reference to `std::istream: :o perator>>(int&)'
/tmp/ccPSfIUf.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp :( .text+0x5f): undefined reference to `std::ios_base::Init::Init()'
main.cpp :( .text+0x76): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccPSfIUf.o :( .eh_frame+0x13): undefined reference to `__gxx_personality_v0'
collect2: error: ld returned 1 exit status
decktone@decktone-pc:~$ nano main.cpp

Za to kiedy zmienię w programie na printf:

Kod: Zaznacz cały

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
printf "123";

int q;
cin >> q;
return 0; 
}

Otrzymuję następujący błąd:

Kod: Zaznacz cały

main.cpp: In function ‘int main()’:
main.cpp:8:1: error: ‘printf’ was not declared in this scope
main.cpp:8:8: error: expected ‘;’ before string constant

Kod: Zaznacz cały

uname -a
Linux decktone-pc 3.2.0-4-686-pae #1 SMP Debian 3.2.46-1+deb7u1 i686 GNU/Linux

Edycja:
Rozwiązane: użycie g++, następnie ./a.out
Awatar użytkownika
pone13
Beginner
Posty: 337
Rejestracja: 30 listopada 2007, 20:59
Lokalizacja: Leszno

Post autor: pone13 »

Dodam na zakończenie że printf się tak nie wywołuje jak napisałeś w przykładzie.
ODPOWIEDZ