Makefile에서 환경변수를 만들수 있는지에 대한 질문입니다.

study의 이미지

제가 하고 싶은 것은 makefile에서 환경변수를 만들고, make가 끝난후에 shell에서 그 변수를 사용하고 싶은데요

예를 들면, 아래와 같이 Makefile에서 MY_VAR라는 환경변수를 만들었습니다.

..
..
MY_VAR = my_value
export MY_VAR
 
 
all: program
   @echo "Inside Makefile: $$MY_VAR"
 
program: program.o 
   $(CC) $(CFLAGS) -o $@ $<

이 makefile은 제가 사용했던 makefile에서 관련 부분만 추출했습니다.
이 Makefile을 수행하면,
"Inside Makefile: my_value"라고 메세지는 출력이 되는데,

$ echo $MY_VAR 를 수행하면 아무 것도 출력이 안되네요
어떻게 하면 makefile에서 만든 환경변수를 shell에서 출력가능할까요?

ymir의 이미지

환경 변수는 parent 에서 child 로만 전달할 수 있습니다.
Makefile 에서 따로 파일에 저장해 놓고, 그걸 다시 읽어서 사용하는 게 나을 것 같네요.

아니면 Makefile 에서 @echo "MY_VAR=$(MY_VAR)" 와 같이 해 놓고..
eval $(make -s) 와 같이 eval 로 make 를 실행하면 바로 현재 쉘에서 쓸 수 있습니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

study의 이미지

아래와 같이 수정해봤습니다.

..
..
MY_VAR = my_value
export MY_VAR
 
 
all: program
   @echo "MY_VAR=$(MY_VAR)"
   eval $(make -s)
 
program: program.o 
   $(CC) $(CFLAGS) -o $@ $<

이렇게 하면 화면에 출력되는 내용은
MY_VAR=my_value
eval
이렇게되고, env를 확인해보면 MY_VAR가 보이지는 않네요.
아직 제가 정확히 이해를 못하는 것 같네요.

ymir의 이미지

제가 설명을 좀 애매하게 했네요.

eval $(make -s) 는 쉘에서 make 할 때 대신 실행하라는 것이었습니다.
make 에 -s 옵션을 쓴 거는, 불필요한 문자열은 출력하지 않고, 필요한 문자열만 출력하게 하기 위해서입니다.
eval 은 화면에 출력되는 문자열을 평가해서 실행해 주는 명령어라..
make 를 할 때 화면에 MY_VAR=my_value 가 출력되면, 이걸 받아서 실행해 주는 거죠.

어쨌거나 make 실행 후에 화면에 출력되는 메시지를 볼 수 없기 때문에, 썩 좋은 방법은 아니지만, 어쨌거나 이런 식으로도 할 수는 있다는 뜻입니다.

되면 한다! / feel no sorrow, feel no pain, feel no hurt, there's nothing gained.. only love will then remain.. 『 Mizz 』

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.