gcc 명령 줄에서 문자열 리터럴을 정의하는 방법은 무엇입니까?
gcc 명령 줄에서 -Dname=Mary
다음 과 같은 문자열을 정의한 다음 소스 코드 printf("%s", name);
에서 인쇄 하려는 Mary
.
어떻게 할 수 있습니까?
두 가지 옵션. 먼저 따옴표를 이스케이프하여 껍질이 먹지 않도록합니다.
gcc -Dname=\"Mary\"
또는 -Dname = Mary를 정말로 원하면 약간 해키이지만 문자열화할 수 있습니다.
#include <stdio.h>
#define STRINGIZE(x) #x
#define STRINGIZE_VALUE_OF(x) STRINGIZE(x)
int main(int argc, char *argv[])
{
printf("%s", STRINGIZE_VALUE_OF(name));
}
STRINGIZE_VALUE_OF는 매크로의 최종 정의까지 기꺼이 평가합니다.
쉘이 따옴표 및 기타 문자를 "먹는"것을 방지하려면 다음과 같이 작은 따옴표를 사용해보십시오.
gcc -o test test.cpp -DNAME='"Mary"'
이렇게하면 정의 된 내용 (따옴표, 공백, 특수 문자 등)을 완전히 제어 할 수 있습니다.
지금까지 찾은 가장 이식 가능한 방법은 사용 \"Mary\"
하는 것입니다. gcc뿐만 아니라 다른 C 컴파일러에서도 작동합니다. 예를 들어 /Dname='"Mary"'
Microsoft 컴파일러와 함께 사용하려고 하면 오류와 함께 중지되지만 /Dname=\"Mary\"
작동합니다.
우분투에서는 CFLAGS를 정의하는 별칭을 사용했고 CFLAGS에는 문자열을 정의하는 매크로가 포함 된 다음 Makefile에서 CFLAGS를 사용합니다. 큰 따옴표 문자와 \ 문자도 이스케이프해야했습니다. 다음과 같이 보입니다.
CFLAGS='" -DMYPATH=\\\"/home/root\\\" "'
다음은 간단한 예입니다.
#include <stdio.h>
#define A B+20
#define B 10
int main()
{
#ifdef __DEBUG__
printf("__DEBUG__ DEFINED\n");
printf("%d\n",A);
#else
printf("__DEBUG__ not defined\n");
printf("%d\n",B);
#endif
return 0;
}
내가 컴파일하면 :
$gcc test.c
산출:
__DEBUG__ not defined
10
내가 컴파일하면 :
$gcc -D __DEBUG__ test.c
산출:
__DEBUG__ defined
30
참고 : 분명히 동일한 시스템에서 동일한 툴체인 심지어 다른 버전 (마찬가지로, 것입니다 ...이 점에서 다른 역할을 할 수 보이는 이 쉘 통과 문제가 될 것입니다,하지만 분명히 그것은 단지 쉘에 제한 아니에요).
Here we have xc32-gcc 4.8.3 vs. (avr-)gcc 4.7.2 (and several others) using the same makefile and main.c, the only difference being 'make CC=xc32-gcc'
, etc.
CFLAGS += -D'THING="$(THINGDIR)/thing.h"'
has been in-use on many versions of gcc (and bash) over several years.
In order to make this compatible with xc32-gcc (and in light of another comment claiming that \" is more portable than '"), the following had to be done:
CFLAGS += -DTHING=\"$(THINGDIR)/thing.h\"
ifeq "$(CC)" "xc32-gcc"
CFLAGS := $(subst \",\\\",$(CFLAGS))
endif
to make things really confusing in discovering this: apparently an unquoted -D with a // results in a #define with a comment at the end... e.g.
THINGDIR=/thingDir/
-> #define /thingDir//thing.h
-> #define /thingDir
(Thanks for the help from answers here, btw).
This is my solution for : -DUSB_PRODUCT=\""Arduino Leonardo\""
I used it in a makefile with:
GNU Make 3.81 (from GnuWin32)
and
avr-g++ (AVR_8_bit_GNU_Toolchain_3.5.0_1662) 4.9.2
The results in a precompiled file (-E option for g++) is:
const u8 STRING_PRODUCT[] __attribute__((__progmem__)) = "Arduino Leonardo";
참고URL : https://stackoverflow.com/questions/2410976/how-to-define-a-string-literal-in-gcc-command-line
'IT Share you' 카테고리의 다른 글
BigDecimal을 사용하여 통화 작업 (0) | 2020.11.17 |
---|---|
java.util.Map의 이름은 어떻게 지정해야합니까? (0) | 2020.11.17 |
Git 로그 출력 로그 파일 (0) | 2020.11.17 |
최소한의 메모리 사용으로 PHP에서 배열과 같은 구조가 필요합니다. (0) | 2020.11.17 |
플라스크 값 오류보기 기능이 응답을 반환하지 않았습니다. (0) | 2020.11.17 |