Microsoft MVP성태의 닷넷 이야기
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 
(연관된 글이 10개 있습니다.)

Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록

C++ thread 객체를 사용했을 때,

#include <thread>
    thread t([&]()

다음과 같은 링크 오류가 발생하는 경우,

Error undefined reference to symbol 'pthread_create@@GLIBC_2.4'
Error DSO missing from command line
Error ld returned 1 exit status

"Configuration Properties" / "Linker" 범주의 "Command Line" 설정에 -pthread 옵션을 추가하는 것으로 해결.




다음과 같은 빌드 오류가,

Error C1020 unexpected #endif

stdafx.h 헤더 파일을 포함하는 코드에서 발생한다면?

#if defined(WIN32)
#include "stdafx.h"
#endif

stdafx.h 헤더 파일이 특별하기 때문입니다. pre-compiled 헤더 파일의 포함 유무 선택은(#if/#endif) 가능하지 않습니다.




Socket의 bind 함수를 추가했는데,

int ret = bind(_sock, (struct sockaddr*)&_server_addr, addr_len);

다음과 같은 오류가 발생한다면?

Error C2440 'initializing': cannot convert from 'std::_Binder<std::_Unforced,int &,sockaddr *,int &>' to 'int'

C++의 bind 함수와 중복되었기 때문입니다. 이런 경우에는 Socket의 bind에 전역 심벌 사용을 명시하면 됩니다.

int ret = ::bind(_sock, (struct sockaddr*)&_server_addr, addr_len);




소켓 사용 시 다음과 같은 오류가 발생한다면?

Error LNK2019 unresolved external symbol ___WSAFDIsSet@8 referenced in function "public: void __thiscall <lambda_5b230eb5c4ebbd478baf88927e86c7be>::operator()(void)const " (??R<lambda_5b230eb5c4ebbd478baf88927e86c7be>@@QBEXXZ)

ws2_32.lib를 import합니다.

#pragma comment(lib, "ws2_32.lib")




socket 함수가 -1을 반환한다면?

_sock = socket(AF_INET, SOCK_DGRAM, 0);

혹시 WSAStartup 호출을 하셨나요? ^^ linux의 경우 소켓 관련 초기화를 할 필요가 없어 자칫 윈도우 용 소스 코드에 누락시켰을 수 있습니다.

#if defined(WIN32)
    WORD wVersionRequested = MAKEWORD(2, 2);
    WSADATA wsaData;

    ::WSAStartup(wVersionRequested, &wsaData);
#endif

    // ... _sock = socket( ... );

#if defined(WIN32)
    WSACleanup();
#endif




samba로 공유시킨 폴더에 윈도우로 접근한 후, notepad를 이용해 .sh 파일을 만든 후 라즈베리 파이에서 실행했더니 다음과 같은 오류가 발생합니다.

$ /share/dual.sh
sudo: unable to execute /share/dual.sh: No such file or directory

리눅스는 execute 권한이 없는 파일을 실행하는 경우 "No such file or directory"라는 오류가 발생합니다. 따라서 다음과 같이 실행 권한을 줘야 합니다.

$ chmod +x /share/dual.sh

또는 이런 오류가 발생할 수도 있습니다.

$ /share/dual.sh
-bash: ./dual.sh: /bin/bash^M: bad interpreter: No such file or directory

왜냐하면 윈도우에서는 개행을 \r\n으로 하지만 리눅스의 경우에는 \n 하나이기 때문입니다. 이미 복사된 환경에서 \r\n을 \n으로 바꿔서 저장하고 싶다면 dos2unix라는 도구를 이용하면 됩니다.

$ sudo apt-get install dos2unix

$ dos2unix dual.sh

또는 Visual Studio와 같은 편집기에서는 "File" / "Save As..." 명령어를 이용해 "Save with Encoding..." 옵션으로 "Line endings"를 "Unix (LF)"로 바꿔 저장하면 됩니다.




리눅스에서는 (Socket을 포함해) File Descriptor에 대한 접근을 잘못하는 경우 "Access Violation" 오류가 발생할 수 있습니다. 가령 다음과 같은 예제를 보면,

struct sockaddr_in server_socket;
socklen_t addr_len = sizeof(server_socket);

int clntSocket = accept(_sock, (struct sockaddr *)&server_socket, &addr_len);

thread clntThread([&]()
{
    char buf[4096] = { 0 };
    int recvBytes = recv(clntSocket, buf, 4096 - 1, 0);
    send(clntSocket, buf, recvBytes, 0);
    closesocket(clntSocket);
});

clntThread.detach();

send 함수 실행에서 다음과 같은 오류와 함께 프로그램이 비정상 종료합니다.

Exception thrown: read access violation.
this->clntSocket was 0x319F500.

왜냐하면, 새롭게 생성된 clntThread보다 먼저 clntSocket 변수가 scope을 벗어났기 때문에 참조(&) 값으로 전달받았던 clntSocket 변수의 값이 스택 변경에 따라 쓰레기 값으로 바뀐 탓입니다. 이런 경우에는 clntSocket 변수에 대한 캡처 방식을 참조가 아닌 복사로 처리하면 됩니다.

struct sockaddr_in server_socket;
socklen_t addr_len = sizeof(server_socket);

int clntSocket = accept(_sock, (struct sockaddr *)&server_socket, &addr_len);

thread clntThread([=]()
{
    char buf[4096] = { 0 };
    int recvBytes = recv(clntSocket, buf, 4096 - 1, 0);

    printf("%s", buf);

    send(clntSocket, buf, recvBytes, 0);
    closesocket(clntSocket);
});

clntThread.detach();




참고로, 람다 함수내에서 참조하는 변수를 명시하지 않으면 이런 컴파일 에러가 발생합니다.

Error E1735 an enclosing-function local variable cannot be referenced in a lambda body unless it is in the capture list

잊지 말고 캡처 변수를 명시하거나 "[&]", "[=]"와 같이 자동 처리하면 됩니다.

그런데, this 포인터를 람다 함수에서 쓰고 싶다면 어떻게 해야 할까요? 그냥 쓰려고 하면 다음과 같은 오류가 발생합니다.

Error C4573 the usage of '...' requires the compiler to capture 'this' but the current default capture mode does not allow it

그렇다고, [&]로 모든 변수를 캡처하고 싶지도 않다면 다음과 같이 this 포인터를 전달할 수 있습니다.

thread clntThread([this](int connectedSocket)




"Visual Studio 2017에서 Raspberry Pi C++ 응용 프로그램 제작"을 하다가 헤더 파일을 include하는 곳에서,

#include "stdafx.h"
#include "VUsbServer.h" // 여기서 "No such file or directory" 에러

다음과 같은 오류가 발생할 수 있습니다.

Error VUsbServer.h: No such file or directory
Error Access to the path 'C:\rasp_vusb\rasp_vusb_server\obj\ARM\Debug\-1607192315.CompileUpToDateFile.tlog' is denied.

분명히 VUsbServer.h 파일이 라즈베리 파이에 정상적으로 복사되어 있는데도 저런 오류가 발생한다면 파일명의 대/소문자까지 맞는지 확인해야 합니다. 윈도우와는 달리 리눅스는 대/소문자까지 일치해야 합니다.




라즈베리 파이에 /etc/dhcp/dhcpd.conf 파일을 이용해 정적 IP 할당 후,

interface wlan0
 static ip_address=192.168.0.50/24
 static routers=192.168.0.1
 static domain_name_servers=164.124.101.2

DNS 이름 풀이를 하는 경우 "Temporary failure in name resolution" 오류가 발생한다면?

$ ping www.sysnet.pe.kr
ping: www.sysnet.pe.kr: Temporary failure in name resolution

물론 domain_name_servers에 지정한 IP가 올바른지도 봐야겠지만, "routers"의 항목도 맞는지 확인을 해야 합니다. 윈도우의 경우 ipconfig으로 보면 "Default Gateway"라는 항목의 IP 주소를 "routers"에 입력해야 하는데요. 습관적으로(?) "xxx.xxx.xxx.1"이라고 지정하면 안됩니다. 경우에 따라 "xxx.xxx.xxx.254"이거나 다른 IP로 얼마든지 설정될 수 있기 때문입니다.



윈도우즈 사용자를 위한 라즈베리 파이 제로 W 모델을 설정하는 방법
; https://www.sysnet.pe.kr/2/0/11372

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 이더넷 카드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11353

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11354

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11355

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스 + 키보드로 쓰는 방법
; https://www.sysnet.pe.kr/2/0/11356

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 마우스로 쓰는 방법 (절대 좌표)
; https://www.sysnet.pe.kr/2/0/11364

Raspberry Pi Zero(OTG)를 다른 컴퓨터에 연결해 가상 키보드 및 마우스로 쓰는 방법 (절대 좌표, 상대 좌표, 휠)
; https://www.sysnet.pe.kr/2/0/11369

라즈베리 파이 용 C++ 프로젝트에 SSL Socket 적용
; https://www.sysnet.pe.kr/2/0/11411

Raspberry Pi/Windows 다중 플랫폼 지원 컴파일 관련 오류 기록
; https://www.sysnet.pe.kr/2/0/11373

Linux 3: 라즈베리 파이 - (윈도우의 NT 서비스처럼) 부팅 시 시작하는 프로그램 설정
; https://www.sysnet.pe.kr/2/0/11374


[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]

[연관 글]






[최초 등록일: ]
[최종 수정일: 7/17/2021]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
12843정성태10/3/20216582스크립트: 29. 파이썬 - fork 시 기존 클라이언트 소켓 및 스레드의 동작파일 다운로드1
12842정성태10/1/202124744오류 유형: 763. 파이썬 오류 - AttributeError: type object '...' has no attribute '...'
12841정성태10/1/20218369스크립트: 28. 모든 파이썬 프로세스에 올라오는 특별한 파일 - sitecustomize.py
12840정성태9/30/20218406.NET Framework: 1119. Entity Framework의 Join 사용 시 다중 칼럼에 대한 OR 조건 쿼리파일 다운로드1
12839정성태9/15/20219485.NET Framework: 1118. C# 11 - 제네릭 타입의 특성 적용파일 다운로드1
12838정성태9/13/20219110.NET Framework: 1117. C# - Task에 전달한 Action, Func 유형에 따라 달라지는 async/await 비동기 처리 [2]파일 다운로드1
12837정성태9/11/20218046VC++: 151. Golang - fmt.Errorf, errors.Is, errors.As 설명
12836정성태9/10/20217627Linux: 45. 리눅스 - 실행 중인 다른 프로그램의 출력을 확인하는 방법
12835정성태9/7/20218937.NET Framework: 1116. C# 10 - (15) CallerArgumentExpression 특성 추가 [2]파일 다운로드1
12834정성태9/7/20217309오류 유형: 762. Visual Studio 2019 Build Tools - 'C:\Program' is not recognized as an internal or external command, operable program or batch file.
12833정성태9/6/20216767VC++: 150. Golang - TCP client/server echo 예제 코드파일 다운로드1
12832정성태9/6/20217580VC++: 149. Golang - 인터페이스 포인터가 의미 있을까요?
12831정성태9/6/20216138VC++: 148. Golang - 채널에 따른 다중 작업 처리파일 다운로드1
12830정성태9/6/20218354오류 유형: 761. Internet Explorer에서 파일 다운로드 시 "Your current security settings do not allow this file to be downloaded." 오류
12829정성태9/5/202110002.NET Framework: 1115. C# 10 - (14) 구조체 타입에 기본 생성자 정의 가능파일 다운로드1
12828정성태9/4/20218129.NET Framework: 1114. C# 10 - (13) 단일 파일 내에 적용되는 namespace 선언파일 다운로드1
12827정성태9/4/20218121스크립트: 27. 파이썬 - 웹 페이지 데이터 수집을 위한 scrapy Crawler 사용법 요약
12826정성태9/3/202110366.NET Framework: 1113. C# 10 - (12) 문자열 보간 성능 개선 [1]파일 다운로드1
12825정성태9/3/20217909개발 환경 구성: 603. GoLand - WSL 환경과 연동
12824정성태9/2/202116987오류 유형: 760. 파이썬 tensorflow - Dst tensor is not initialized. 오류 메시지
12823정성태9/2/20216730스크립트: 26. 파이썬 - PyCharm을 이용한 fork 디버그 방법
12822정성태9/1/202111935오류 유형: 759. 파이썬 tensorflow - ValueError: Shapes (...) and (...) are incompatible [2]
12821정성태9/1/20217469.NET Framework: 1112. C# - .NET 6부터 공개된 ISpanFormattable 사용법
12820정성태9/1/20217786VC++: 147. Golang - try/catch에 대응하는 panic/recover [1]파일 다운로드1
12819정성태8/31/20217905.NET Framework: 1111. C# - FormattableString 타입
12818정성태8/31/20217145Windows: 198. 윈도우 - 작업 관리자에서 (tensorflow 등으로 인한) GPU 연산 부하 보는 방법
... [31]  32  33  34  35  36  37  38  39  40  41  42  43  44  45  ...