실행중인 명령어가 64비트인지, 32비트인지 어떻게 구별할 수 있을까요 ?

ukyoukyo의 이미지



Linux, GCC 환경입니다.

64비트 OS를 설치했구요,

빌드는 32비트, 64비트 각각 개별 실행파일로 되게했습니다.


즉, 하나의 hello.c를 빌드하면 hello32, hello64 실행파일이 각각 만들어 집니다.
( gcc -m32, -m64 옵션을 사용했습니다 )



질문은,

프로그램 실행중에( runtime ) 프로그램 자신이 32비트인지, 64비트인지 구별하는 함수가 있을까요 ?

없다면 대략을 요령이라도 알려주심 감사하겠습니다...


실행 파일이름으로 구별하는건 파일이름을 사용자가 임의로 변경할 수 있으므로 제외하구요...


readelf 등등의 명령어를 보긴 봤는데, 도움이 안되는것 같아서요...

qiiiiiiiip의 이미지

일반적인 경우라면 pointer의 sizeof로 알 수 있습니다.

if ( sizeof( int* )  == 8 ) do64();
else if (sizeof( int* ) == 4 ) do32();
else error();

이렇게 확인이 안되는 예외적인 경우가 있는지는 잘 모르겠네요.

snowall의 이미지

어차피 bit가 안맞으면 실행이 안될테니 지금 사용하는 프로세서 정보를 참조하는건 어떤가요?

피할 수 있을때 즐겨라! http://melotopia.net/b

qiiiiiiiip의 이미지


32bit lib를 설치한 64bit 머신에서는 32bit app이 잘 돌아갑니다만...

snowall의 이미지

그러네요.

생각해봤는데, 바이너리의 명령어 비트 수는 컴파일 시점에서 결정되니까, 컴파일러가 64비트로 컴파일 할 때랑 32비트로 컴파일 할 때, 매크로를 사용해서 정해주면 되는 것 같네요.

그래서 찾아보니 있군요...

http://stackoverflow.com/questions/5272825/detecting-64bit-compile-in-c

피할 수 있을때 즐겨라! http://melotopia.net/b

ukyoukyo의 이미지


빌드타임때 -DBIT32, -DBIT64 처럼 매크로를 삽입하는 방법으로 해결했습니다.

알려주셔서 감사드립니다.


------------------ System programmer...^^

댓글 달기

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
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.