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)
12719정성태7/19/20216128오류 유형: 737. SharePoint 설치 시 "0x800710D8 The object identifier does not represent a valid object." 오류 발생
12718정성태7/19/20216750개발 환경 구성: 581. Windows에서 WSL로 파일 복사 시 root 소유권으로 적용되는 문제파일 다운로드1
12717정성태7/18/20216726Windows: 195. robocopy에서 파일의 ADS(Alternate Data Stream) 정보 복사를 제외하는 방법
12716정성태7/17/20217511개발 환경 구성: 580. msbuild의 Exec Task에 robocopy를 사용하는 방법파일 다운로드1
12715정성태7/17/20219230오류 유형: 736. Windows - MySQL zip 파일 버전의 "mysqld --skip-grant-tables" 실행 시 비정상 종료 [1]
12714정성태7/16/20217937오류 유형: 735. VCRUNTIME140.dll, MSVCP140.dll, VCRUNTIME140.dll, VCRUNTIME140_1.dll이 없어 exe 실행이 안 되는 경우
12713정성태7/16/20218483.NET Framework: 1077. C# - 동기 방식이면서 비동기 규약을 따르게 만드는 Task.FromResult파일 다운로드1
12712정성태7/15/20217887개발 환경 구성: 579. Azure - 리눅스 호스팅의 Site Extension 제작 방법
12711정성태7/15/20218228개발 환경 구성: 578. Azure - Java Web App Service를 위한 Site Extension 제작 방법
12710정성태7/15/20219996개발 환경 구성: 577. MQTT - emqx.io 서비스 소개
12709정성태7/14/20216674Linux: 42. 실행 중인 docker 컨테이너에 대한 구동 시점의 docker run 명령어를 확인하는 방법
12708정성태7/14/202110026Linux: 41. 리눅스 환경에서 디스크 용량 부족 시 원인 분석 방법
12707정성태7/14/202177237오류 유형: 734. MySQL - Authentication method 'caching_sha2_password' not supported by any of the available plugins.
12706정성태7/14/20218555.NET Framework: 1076. C# - AsyncLocal 기능을 CallContext만으로 구현하는 방법 [2]파일 다운로드1
12705정성태7/13/20218674VS.NET IDE: 168. x64 DLL 프로젝트의 컨트롤이 Visual Studio의 Designer에서 보이지 않는 문제 - 두 번째 이야기
12704정성태7/12/20217814개발 환경 구성: 576. Azure VM의 서비스를 Azure Web App Service에서만 접근하도록 NSG 설정을 제한하는 방법
12703정성태7/11/202113448개발 환경 구성: 575. Azure VM에 (ICMP) ping을 허용하는 방법
12702정성태7/11/20218577오류 유형: 733. TaskScheduler에 등록된 wacs.exe의 Let's Encrypt 인증서 업데이트 문제
12701정성태7/9/20218233.NET Framework: 1075. C# - ThreadPool의 스레드는 반환 시 ThreadStatic과 AsyncLocal 값이 초기화 될까요?파일 다운로드1
12700정성태7/8/20218627.NET Framework: 1074. RuntimeType의 메모리 누수? [1]
12699정성태7/8/20217457VS.NET IDE: 167. Visual Studio 디버깅 중 GC Heap 상태를 보여주는 "Show Diagnostic Tools" 메뉴 사용법
12698정성태7/7/202111428오류 유형: 732. Windows 11 업데이트 시 3% 또는 0%에서 다운로드가 멈춘 경우
12697정성태7/7/20217326개발 환경 구성: 574. Windows 11 (Insider Preview) 설치하는 방법
12696정성태7/6/20217879VC++: 146. 운영체제의 스레드 문맥 교환(Context Switch)을 유사하게 구현하는 방법파일 다운로드2
12695정성태7/3/20217960VC++: 145. C 언어의 setjmp/longjmp 기능을 Thread Context를 이용해 유사하게 구현하는 방법파일 다운로드1
12694정성태7/2/20219869Java: 24. Azure - Spring Boot 앱을 Java SE(Embedded Web Server)로 호스팅 시 로그 파일 남기는 방법 [1]
... 31  32  33  34  35  [36]  37  38  39  40  41  42  43  44  45  ...