[완료]메인 소스에서 다른 파일의 소스 링크하려면..??

ONLYLOVE의 이미지

제목대로...

test1.c에 있는 메인 프로그램에서 test.2에 있는 test()함수를 불러오려합니다..

이하 test1.c 내용
#include

int main()
{
test();
}

이하 test2.c 내용
int test()
{
printf("hello world\n");
}

1. 먼저 test2.c를 컴파일하여 object파일을 생성합니다.
#gcc test2.c -o test2.o

2. 그리고 메인소스에서 test2.o 파일을 불러다 쓰려고 합니다.
#gcc test1.c -o test1.o test2.o

3. 그러면 결과...

아래와 같은 에러가 발생합니다.
test2.o: In function `_init':
test2.o(.init+0x0): multiple definition of `_init'
/usr/qnx630/target/qnx6/x86/lib/crti.o(.init+0x0): first defined here
test2.o: In function `_start':
test2.o(.text+0x8): multiple definition of `_start'
/usr/qnx630/target/qnx6/x86/lib/crt1.o(.text+0x8): first defined here
test2.o: In function `_fini':
test2.o(.fini+0x0): multiple definition of `_fini'
/usr/qnx630/target/qnx6/x86/lib/crti.o(.fini+0x0): first defined here
test2.o: In function `__PIC__':
test2.c(*ABS*+0x8049548): multiple definition of `_GLOBAL_OFFSET_TABLE_'
/usr/qnx630/target/qnx6/x86/lib/crt1.o(.got.plt+0x0): first defined here
/usr/qnx630/target/qnx6/x86/lib/crt1.o(.dynamic+0x0): multiple definition of `_DYNAMIC'
collect2: ld returned 1 exit status

잘못된점이 있다면... 날카로운 지적 부탁드립니다(--)(__)

neogeo의 이미지

test1.c 에서 test() 함수의 원형을 알 수 있도록 선언되어야 합니다.

보통은 extern 이나 header 파일을 이용하여 저 test() 함수의 원형을 test2.c 에서 알 게 되겠지요.

test1.h 파일을 작성하시거나 test2.c 에서 extern 으로 test() 함수를 선언해 주십시오.

Neogeo - Future is Now.

Neogeo - Future is Now.

bushi의 이미지

qnx ... 로군요. gcc 를 잘 못 사용하셨습니다.

micro kernel 이라 링커가 좀 묘하게 만들어졌습니다.
main() 이 소스에 없어도 멀쩡히 컴파일/링크가 되지요.
이 경우 커널 모듈/데몬 취급을 당합니다.

쉽게 말해,

gcc test2.c -o test2.o

는 일반 리눅스 였다면 main() 가 없으므로 링크에러가 발생해야 마땅합니다.
단순히 오브젝트만 만드실거라면
gcc -c test2.c -o test2.o

라고 해야하니까요.

하지만 qnx 다 보니, 링크에러가 발생하는 대신 결과물이 커널 모듈/데몬으로 만들어집니다.
이 경우, main() 이 명시적으로 없어도 되고, 실행파일이 됩니다.

-c 옵션 추가해서 test.o 를 만드세요.

OTL

ONLYLOVE의 이미지

헌데 제가 적은 위 예시는 한개의 오브젝트 파일을 링크한 경우인데..

저같은 경우는..

한모듈당 100개의 *.c 파일을 한개의 module1.o 파일로 만들어서

다섯개의 모듈을 메인에 추가하려할때 생기는 문제입니다.

gcc -c main.c -o main module1.o module2.o module3.o module4.o module5.o

이 경우 두번 링크되려해서 생기는 문제인듯한데..

500개의 소스를 한 디렉터리에 넣고 링크하기엔... 어지럽고 지저분할듯한데..

다른방법이 있다면 질문드립니다.

----------------------------------------------------
I can do everthing through him who give me strength.

bushi의 이미지

틀렸습니다.

링커가 그정도로 멍청하지는 않습니다.
오브젝트들에 포함된 심볼이 scope 를 넘어서 중복될 수는 있지만, 에러메시지를 보면 이것도 아닙니다.

startup(bootstrap) 코드가 중복되는 경우는 앞서 말씀드린 이유, 그것 하나 뿐입니다.
qnx 가 아니었다면 .o 오브젝트를 컴파일 할 당시에 이미 에러가 발생했을 것 입니다. main() 이 없다고.

Makefile 을 작성하셔서 make 를 사용하세요.
소스가 한개건 천개건 십만개건, 파일 갯수가 무슨 상관이겠습니까.
리눅스 커널(2.6.23.12)엔 9474 개의 .c 파일이 있습니다.

좀 오래되긴 했지만... 제 기억엔 QNX 는 거의 완벽한 문서가 제공됩니다.
IDE 를 사용하면 모든 도움말과 예제를 보실 수 있고요(java 라 좀 느리지만...)
Makefile 에 대한 도움말이 있는 지 모르겠는데... 없다면 GNU make 문서를 보셔도 됩니다.

OTL

ONLYLOVE의 이미지

말씀하신데론 시도를 해봤는데.. 결국안돼서 다른방법을 간구했습니다..
gcc에서도 라이브러리를 생성해서 사용할수가 있더군요^^
조언 감사드립니다. 덕분에 해결할수있었어요~

----------------------------------------------------
I can do everthing through him who give me strength.

댓글 달기

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