문제

{net04:~/xxxx/wip} gcc -o  write_test write_test.c
In file included from write_test.c:4:
global.h:10: warning: `b' initialized and declared `extern'

이 코드는 fcntl.h를 사용하고 Open (), write (), close () 등과 같이 정의 된 파일 처리 함수를 사용합니다. 코드는 컴파일하고 의도 된대로 작동합니다.

{net04:~/xxxx/wip} gcc -o  write_test write_test.cpp
In file included from write_test.cpp:4:
global.h:10: warning: `b' initialized and declared `extern'
write_test.cpp: In function `int main()':
write_test.cpp:56: error: `exit' undeclared (first use this function)
write_test.cpp:56: error: (Each undeclared identifier is reported only once for each function it appears in.)
write_test.cpp:58: error: `write' undeclared (first use this function)
write_test.cpp:62: error: `close' undeclared (first use this function)

CPP 소스 코드로 사용하면 GCC가 불만을 제기하는 이유는 무엇입니까? 그리고 흥미롭게도 Open ()에 대해 불평하지 않는 이유는 무엇입니까? 여기서 무슨 일이 일어나고 있습니까?

도움이 되었습니까?

해결책

  1. C ++는 헤더에 대해 더 엄격합니다. 필요합니다. #include <unistd.h> 기능을 올바르게 얻으려면 표시됩니다.

  2. global.h B- 헤더가 변수를 초기화해서는 안됩니다.

  3. 컴파일 할 때 사용해야합니다 -Wall -Werror 그리고 그것은 당신이 당신의 코드의 모든 dodgy 비트를 고치도록 강요 할 것입니다.

  4. 얻기 위해 exit() 깨끗하게 필요합니다 #include <cstdlib> (C ++) 또는 #include <stdlib.h> (씨)

  5. 사용 g++ C ++ 라이브러리가 포함되도록 C ++ 코드를 연결합니다. 전체 C ++ 컴파일을 수행하는 것이 가장 쉬운 g++.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top