MinGW 에서 linker error

ichoong의 이미지

Windows XP에 MinGW를 설치해서
프로그램을 좀 짜보려고 하는데요.
제가 리눅스나 gnu tool에 익숙하지 않아서 그러는데요.

아주 기본적인 프로그램을 짜고 gnu linker로 link를 시도하면 error가 나네요.

예를들어

#include
int main()
{
printf("hello\n");
}

라는 test.c 파일을
> gcc -c test.c
로 컴파일 하고

> ld -o test test.o
를 하면
test.o:test.c:(.text+0x21): undefined reference to `_alloca
test.o:test.c:(.text+0x26): undefined reference to `__main'
test.o:test.c:(.text+0x32): undefined reference to `printf'

이런 에러가 나면서 링크가 안되네요.

> ld -o test -Ttext 0x0 -e main test.o
인터넷에서 위와같이 실행하는 예제가 있어서
이렇게 해도 마찬가지로 에러가 납니다.

그냥 gcc로 -o option주고 link까지 하면 잘 컴파일되고 실행됩니다.

혹시나해서 cygwin을 설치해서 해봐도 같은 에러가 발생하는군요.

제가 뭘 잘못알고 있나요?

grassman의 이미지

ld로 링크 시킨다면 두 작업을 다 해줘야 합니다. 따라서 gcc를 통해서 링크시키는 방법이 최적입니다. 특별한 이유가 있어서 startup stub을 무시해야 한다거나 할 경우에만 ld를 통해 직접 링크하시기 바랍니다.

bushi의 이미지

gcc 에 -v 옵션을 붙이면 컴파일과 링크과정을 볼 수 있습니다.
궁금하다면, 링커가 어떤 옵션으로 실행되는 지 살펴보세요.

[bushi@rose net]$ gcc -v -o r r.c
Using built-in specs.
Target: i386-redhat-linux
Configured with: ../configure --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --enable-shared --enable-threads=posix --enable-checking=release --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-languages=c,c++,objc,obj-c++,java,fortran,ada --enable-java-awt=gtk --disable-dssi --enable-plugin --with-java-home=/usr/lib/jvm/java-1.5.0-gcj-1.5.0.0/jre --enable-libgcj-multifile --enable-java-maintainer-mode --with-ecj-jar=/usr/share/java/eclipse-ecj.jar --with-cpu=generic --host=i386-redhat-linux
Thread model: posix
gcc version 4.1.2 20070925 (Red Hat 4.1.2-33)
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/cc1 -quiet -v r.c -quiet -dumpbase r.c -mtune=generic -auxbase r -version -o /tmp/ccUUfjnt.s
ignoring nonexistent directory "/usr/lib/gcc/i386-redhat-linux/4.1.2/../../../../i386-redhat-linux/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/local/include
 /usr/lib/gcc/i386-redhat-linux/4.1.2/include
 /usr/include
End of search list.
GNU C version 4.1.2 20070925 (Red Hat 4.1.2-33) (i386-redhat-linux)
        compiled by GNU C version 4.1.2 20070925 (Red Hat 4.1.2-33).
GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: ab322ce5b87a7c6c23d60970ec7b7b31
 as -V -Qy -o /tmp/ccCAHoAs.o /tmp/ccUUfjnt.s
GNU assembler version 2.17.50.0.18 (i386-redhat-linux) using BFD version version 2.17.50.0.18-1 20070731
 /usr/libexec/gcc/i386-redhat-linux/4.1.2/collect2 --eh-frame-hdr --build-id -m elf_i386 --hash-style=gnu -dynamic-linker /lib/ld-linux.so.2 -o r /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crt1.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crti.o /usr/lib/gcc/i386-redhat-linux/4.1.2/crtbegin.o -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2 -L/usr/lib/gcc/i386-redhat-linux/4.1.2/../../.. /tmp/ccCAHoAs.o -lgcc --as-needed -lgcc_s --no-as-needed -lc -lgcc --as-needed -lgcc_s --no-as-needed /usr/lib/gcc/i386-redhat-linux/4.1.2/crtend.o /usr/lib/gcc/i386-redhat-linux/4.1.2/../../../crtn.o
[bushi@rose net]$ 

OTL

anfl의 이미지

ld -m elf_i386 -Ttext 0x0 -e main -o test [원도우즈 패스]/crt1.o [원도우즈 패스]/crti.o [원도우즈 패스]/crtn.o test.o -lc

-Ttext 0x0 -e main 옵션을 왜 넣으신지 모르겠지만 뻗을 겁니다.

text의 위치가 0x0가 되면 일단 뻗고, entry가 main이 되면 종료할때 뻗습니다.


winhee의 이미지

오래된 질문이지만 타인을 위해 답변드립니다.
저또한 windows 환경에서 cygwin을 사용해서 작업중 같은 난관에 봉착했습니다.
위의 님의 답변을 따라 컴파일 과정을 따라가 보니 collect2 에 생소한 링크가 있더군요 -lcygwin이라는..
결국
ld -o test -Ttext 0x0 -e main test.o -lcygwin
으로 링크 해결은 봤습니다만
-e main 옵션으로 엔트리 포인트를 찾지 못하는 불상사가..그래서 그냥 지워버리고
ld -o test -Ttext 0x0 test.o -lcygwin
으로 정리했습니다.
ndisasm 역시 OSRC예제와는 조금 많이 다르게 어셈블러 코드가 상당히 길고(비록 30줄내외로 ret포인트가 있지만, 그 밑으로도 여러 쓸데없는듯한 어셈들이 많이 있다는..) 종잡을수 없지만..
아무튼 대충적으로는 넘어갈수있는 상태인듯..

댓글 달기

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