반응형
플레인 파일을 실행 파일에 링크할 수 있습니까?
일부 프레임워크(Qt, Windows, Gtk...)는 바이너리에 리소스를 추가하는 기능을 제공합니다.정말 필요한 것은 이것뿐이기 때문에 프레임워크 없이 이것을 달성할 수 있을까?
- 이진(데이터 세그먼트) 내에 리소스 주소를 포함하는 기호
- 자원의 길이를 나타내는 기호
- 자원 자체
gcc 툴체인을 사용하여 이를 실현하려면 어떻게 해야 합니까?
다음과 같이 할 수 있습니다.
objcopy --input binary \
--output elf32-i386 \
--binary-architecture i386 my_file.xml myfile.o
그러면 실행 파일에 연결할 수 있는 개체 파일이 생성됩니다.이 파일에는 C 코드에 선언해야 사용할 수 있는 이러한 기호가 포함됩니다.
00000550 D _binary_my_file_xml_end
00000550 A _binary_my_file_xml_size
00000000 D _binary_my_file_xml_start
가장 기본적인 것은 다음과 같습니다.char
바이트로 가득 찬 배열
Linux 에서는 다음을 사용할 수 있습니다.xxd -i <file>
파일을 에 "삽입"하다char
어레이를 바이너리에 링크하고 구성 바이트를 원하는 대로 사용합니다.
여기 제 코드의 예가 있습니다.makefile
, 라고 하는 「리소스 파일」을 작성합니다.templates.h
다발을 포함하는 모습char
HTML 템플릿을 나타내는 배열:
templates.h:
@echo "#ifndef REDACTED_TEMPLATES_H" > templates.h
@echo "#define REDACTED_TEMPLATES_H" >> templates.h
@echo "// Auto-generated file! Do not modify!" >> templates.h
@echo "// NB: arrays are not null-terminated" >> templates.h
@echo "// (anonymous namespace used to force internal linkage)" >> templates.h
@echo "namespace {" >> templates.h
@echo "namespace templates {" >> templates.h
@cd templates;\
for i in * ;\
do \
echo "Compiling $$i...";\
xxd -i $$i | sed -e 's/ =/ __attribute__((unused)) =/' >> ../templates.h;\
done;\
cd ..
@echo "}" >> templates.h
@echo "}" >> templates.h
@echo "#endif" >> templates.h
(또, 다음의 항목도 참조해 주세요.이러한 자동 생성된 오브젝트에 프로그래밍 방식으로 '_attribute__'(미사용)를 적용하는 최선의 방법은 무엇입니까?
결과는 다음과 같습니다.
#ifndef REDACTED_TEMPLATES_H
#define REDACTED_TEMPLATES_H
// Auto-generated file! Do not modify!
// NB: arrays are not null-terminated
// (anonymous namespace used to force internal linkage)
namespace {
namespace templates {
unsigned char alert_email_finished_events_html[] __attribute__((unused)) = {
0x3c, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73,
0x3d, 0x22, 0x6e, 0x6f, 0x64, 0x65, 0x2d, 0x69, 0x6e, 0x66, 0x6f, 0x2d,
[..]
0x7d, 0x7d, 0x0d, 0x0a, 0x3c, 0x2f, 0x74, 0x61, 0x62, 0x6c, 0x65, 0x3e,
0x0d, 0x0a
};
unsigned int alert_email_finished_events_html_len __attribute__((unused)) = 290;
unsigned char alert_email_finished_events_list_html[] __attribute__((unused)) = {
0x3c, 0x74, 0x72, 0x20, 0x63, 0x6c, 0x61, 0x73, 0x73, 0x3d, 0x22, 0x73,
0x65, 0x70, 0x61, 0x72, 0x61, 0x74, 0x65, 0x2d, 0x70, 0x72, 0x65, 0x76,
[..]
0x73, 0x74, 0x7d, 0x7d, 0x0d, 0x0a
};
unsigned int alert_email_finished_events_list_html_len __attribute__((unused)) = 42;
}
}
#endif
이 예는 하나의 변환 유닛에서만 리소스를 사용하는 경우에 최적입니다만, 일반적인 접근방식은 고객의 요구에 맞추어 조정할 수 있습니다.
언급URL : https://stackoverflow.com/questions/13856930/can-i-link-a-plain-file-into-my-executable
반응형
'sourcecode' 카테고리의 다른 글
VueX를 통한 데이터 액세스 (0) | 2022.08.31 |
---|---|
x == (x = y)가 (x = y) == x와 같지 않은 이유는 무엇입니까? (0) | 2022.08.31 |
Linux에서 C에 디렉토리를 반복적으로 나열하는 방법 (0) | 2022.08.31 |
"메인 클래스를 찾을 수 없거나 로드할 수 없습니다"는 무슨 의미입니까? (0) | 2022.08.31 |
포인터 캐스팅 시 얼라인먼트에 대해 걱정해야 합니까? (0) | 2022.08.30 |