gcc로 C ++ 프로그램 컴파일
질문 : gcc 컴파일러로 C ++ 프로그램을 컴파일하는 방법은 무엇입니까?
info.c :
#include<iostream>
using std::cout;
using std::endl;
int main()
{
#ifdef __cplusplus
cout << "C++ compiler in use and version is " << __cplusplus << endl;
#endif
cout <<"Version is " << __STDC_VERSION__ << endl;
cout << "Hi" << __FILE__ << __LINE__ << endl;
}
그리고 컴파일하려고 할 때 info.c
$ gcc info.C
Undefined first referenced
symbol in file
cout /var/tmp/ccPxLN2a.o
endl(ostream &) /var/tmp/ccPxLN2a.o
ostream::operator<<(ostream &(*)(ostream &))/var/tmp/ccPxLN2a.o
ostream::operator<<(int) /var/tmp/ccPxLN2a.o
ostream::operator<<(long) /var/tmp/ccPxLN2a.o
ostream::operator<<(char const *) /var/tmp/ccPxLN2a.o
ld: fatal: Symbol referencing errors. No output written to a.out
collect2: ld returned 1 exit status
gcc 컴파일러는 C ++ 프로그램을 컴파일 할 수 없습니까? 관련 메모에서 gcc와 g ++의 차이점은 무엇입니까? 감사,
gcc는 실제로 C ++ 코드를 잘 컴파일 할 수 있습니다. 받은 오류는 컴파일러 오류가 아니라 링커 오류입니다.
컴파일 라인을 다음과 같이 변경하면 확률이 높습니다.
gcc info.C -lstdc++
표준 C ++ 라이브러리에 연결하면 제대로 작동합니다.
그러나 당신은 당신의 삶을 더 쉽게 만들고 g ++를 사용해야합니다.
편집하다:
Rup 은 다른 답변에 대한 그의 의견 에서 가장 잘 말합니다 .
[...] gcc는 파일 확장자에 따라 올바른 백엔드 컴파일러를 선택하고 (즉, .c를 C로, .cc를 C ++로 컴파일) 기본적으로 표준 C 및 GCC 도우미 라이브러리에 대해 바이너리를 연결합니다. 입력 언어; g ++는 또한 모든 C 소스를 C ++로 컴파일하고 (즉, .c와 .cc를 C ++로 컴파일 함), 입력 언어에 관계없이 링크 단계에 libstdc ++를 포함한다는 점을 제외하고는 확장을 기반으로 올바른 백엔드를 선택합니다.
코드에 .c 확장자를 지정하면 컴파일러는 C ++가 아닌 C 코드라고 생각합니다. C ++ 컴파일러 드라이버는 g ++라고합니다. gcc 드라이버를 사용하면 표준 C ++ 라이브러리가 기본적으로 링크되지 않으므로 링커 문제가 발생합니다. 따라서 원하는 :
g++ myprog.cpp
코드를 이식하고 싶지 않고 함께 일하는 사람들에게 미움을받을 준비가되어 있지 않다면 대문자 .C 확장자를 사용하는 것도 고려하지 마십시오.
사용하는 g++
대신 gcc
.
gcc와 g ++의 차이점은 다음과 같습니다.
gcc | g++
compiles c source | compiles c++ source
gcc 대신 g ++를 사용하여 C ++ 소스를 컴파일하십시오.
기본적으로 gcc는 파일 확장자에 따라 언어를 선택하지만 gcc가 -x 옵션을 사용하여 다른 언어 백엔드를 선택하도록 강제 할 수 있습니다.
gcc -x c++
더 많은 옵션은 "출력 종류를 제어하는 옵션"아래의 gcc 맨 페이지에 자세히 설명되어 있습니다. 예를 들어 http://linux.die.net/man/1/gcc (페이지에서 텍스트 검색)를 참조하십시오 -x language
.
이 기능은 gcc가 파일 확장자를 사용하여 언어를 추측 할 수없는 경우에 매우 유용합니다. 예를 들어 코드를 생성하고 stdin을 통해 gcc에 공급하는 경우입니다.
It worked well for me. Just one line code in cmd.
First, confirm that you have installed the gcc (for c) or g++ (for c++) compiler.
In cmd for gcc type:
gcc --version
in cmd for g++ type:
g++ --version
If it is installed then proceed.
Now, compile your .c or .cpp using cmd
for .c syntax:
gcc -o exe_filename yourfilename.c
Example:
gcc -o myfile myfile.c
Here exe_filename (myfile in example) is the name of your .exe file which you want to produce after compilation (Note: i have not put any extension here). And yourfilename.c (myfile.c in example) is the your source file which has the .c extension.
Now go to folder containing your .c file, here you will find a file with .exe extension. Just open it. Hurray..
For .cpp syntax:
g++ -o exe_filename yourfilename.cpp
After it the process is same as for .c .
If I recall correctly, gcc determines the filetype from the suffix. So, make it foo.cc and it should work.
And, to answer your other question, that is the difference between "gcc" and "g++". gcc is a frontend that chooses the correct compiler.
참고URL : https://stackoverflow.com/questions/3178342/compiling-a-c-program-with-gcc
'program story' 카테고리의 다른 글
ID가 'com.google.gms.google-services'인 플러그인을 찾을 수 없습니다. (0) | 2020.11.16 |
---|---|
프로덕션에 업로드하려고 할 때 Android 서명 APK가 서명되지 않은 APK로 표시됨 (0) | 2020.11.16 |
NSData 또는 UIImage에서 이미지 유형 찾기 (0) | 2020.11.16 |
Google지도 API V3 메서드 fitBounds () (0) | 2020.11.16 |
C #의 Zip 폴더 (0) | 2020.11.16 |