gcc 사용 질문

duty7351의 이미지

헤더파일은 Point.h ArrayList.h 이고
소스파일은 ArrayList.c 입니다. 그리고 ArrayList.c는 위의 두 헤더파일을 사용합니다.

그래서 제가
$ gcc -c Point.h ArrayList.h ArrayList.c -o ArrayList.o 라고 작성을 했습니다

근데 오류가 다음과 같이 뜹니다.

gcc: fatal error: cannot specify ‘-o’ with ‘-c’, ‘-S’ or ‘-E’ with multiple files
compilation terminated.

제가 의도한 바는 "Point.h ArrayList.h ArrayList.c 를 컴파일 하여 output file명을 ArrayList.o 로 만들고 싶다" 였는데 잘 안되네요 ㅠㅠ

근데 $gcc -c Point.h ArrayList.h ArrayList.c -c 는 정상적으로 컴파일이 됩니다.

의도한 -o 가 왜 여러개의 파일이 중복(?)되는지 궁금합니다!

익명 사용자의 이미지

그냥 gcc -c ArrayList.c -o ArrayList.o 하면 됩니다.

duty7351의 이미지

같은 경로에 헤더파일이 있어서 그렇게 사용할 수 있군요!

사실 Makefile 작성중에 오류가 났었습니다. 단편적으로 gcc 사용법을 올린것 같네요. 제가 Makefile을 다음과 같이 짰는데 한번 봐주실 수 있으신가요?

CC = gcc
TARGET = main
SRCS = $(wildcard *.c)
INC = $(wildcard *.h)
OBJS = $(SRCS:.c=.o)

all: $(TARGET)

$(TARGET): $(OBJS)
$(CC) $(INC) -o $@

%.o: %.c
$(CC) $(INC) -c $< -o $@ <------- 이부분이 문제입니다 -o $@를 빼면 정상작동 합니다.

.PHONY: clean
clean:
rm -rf $(OBJS) $(TARGET) $(wildcard ./*.gch)

익명 사용자의 이미지

$(CC) -c $< -o $@

duty7351의 이미지

같은 경로에 있는 헤더파일을 굳이 컴파일 대상에 포함 안시켜도 되는군요
만약에 헤더파일을 include 폴더에 넣었다면

INC = -I./include 로 바꾸고

$(CC) $(INC) -c $< -o $@ 가 되겠네요(?)

익명 사용자의 이미지

(전통적인 의미에서의) C/C++ 헤더 파일은 직접적으로 컴파일의 대상이 되지 않습니다.

다른 소스 코드 파일에서 include할 경우 거기서 컴파일 될 뿐이죠.

gcc에서 억지로 헤더 파일에 컴파일 명령을 내리면, 원하는 것과 약간 다른 일이 일어나게 됩니다:

https://en.wikipedia.org/wiki/Precompiled_header

======

소스 코드가 include하는 헤더 파일을 찾을 수 있게 -I 플래그만 잘 주면 됩니다.

duty7351의 이미지

감사합니다! 열심히 문서 정독 하겠습니다~

댓글 달기

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