私は新しいので、main.cクラスでテスターのメソッドを使用しようとしています。だからここ含まれているヘッダーで宣言されたメソッドを使用するときにcでリンクするときにエラーが発生しました
はmain.cのである:ここでは
#include <stdbool.h>
#include <stdio.h>
#include "tester.h"
int main(void) {
bool is_valid = isTrue();
}
はtester.hです:ここでは
#include <stdbool.h>
bool isTrue();
はtester.cです:
#include "tester.h"
bool isTrue() {
return true;
}
そして、ここでは何が起こるかです私はコンパイルしようとします:
$ make main tester
gcc -g -O0 -Wall --std=c99 -pedantic -g -O0 main.c -o main
main.c: In function ‘main’:
main.c:7:10: warning: unused variable ‘is_valid’ [-Wunused-variable]
bool is_valid = isTrue();
^
/tmp/ccwIzgJQ.o: In function `main':
/home/paul/CS261/p1-check/mess/main.c:7: undefined reference to `isTrue'
collect2: error: ld returned 1 exit status
make: *** [main] Error 1
私の教授が私のMakefileを提供しました。私はここに内容を掲載することができますが、それが正しいと確信しています。ここでリンクエラーが発生していますが、なぜですか?私はmain.cにtester.hファイルを含めたので、定義してはいけませんか?援助は非常に高く評価されるでしょう。
編集:ここではMakefileのです:私はリンクのためのMakefileを使用しようとすると
# Simple Makefile
#
#
# This makefile builds a simple application that contains a main module
# (specified by the EXE variable) and a predefined list of additional modules
# (specified by the MODS variable). If there are any external library
# dependencies (e.g., the math library, "-lm"), list them in the LIBS variable.
# If there are any precompiled object files, list them in the OBJS variable.
#
# By default, this makefile will build the project with debugging symbols and
# without optimization. To change this, edit or remove the "-g" and "-O0"
# options in CFLAGS and LDFLAGS accordingly.
#
# By default, this makefile build the application using the GNU C compiler,
# adhering to the C99 standard with all warnings enabled.
# application-specific settings and run target
EXE=y86
MODS=p1-check.o
OBJS=
LIBS=
default: $(EXE)
test: $(EXE)
make -C tests test
# compiler/linker settings
CC=gcc
CFLAGS=-g -O0 -Wall --std=c99 -pedantic
LDFLAGS=-g -O0
# build targets
$(EXE): main.o $(MODS) $(OBJS)
$(CC) $(LDFLAGS) -o $(EXE) $^ $(LIBS)
%.o: %.c
$(CC) -c $(CFLAGS) $<
clean:
rm -f $(EXE) main.o $(MODS)
make -C tests clean
.PHONY: default clean
私は、コマンドの一部が欠けていますか?それはおそらく私の最終的なものですが、私は何がわかりません。
「しかし私はそれが正しいと確信しています。」とは言いませんが、それは正しくありません。 –
'gcc -g -O0 -WALL --std = c99 -pedantic -g -O0 main.c -o main'は' gcc -g -O0 -Wall --std = c99 -pedantic -g -O0 mainでなければなりません。 c tester.c -o main' –
いいえ、あなたはmakefileを表示する必要があります。別の 'tester.c'ファイルを使うのは間違っているか、設定されていません。宣言(ヘッダーファイル)と定義(Cファイル)の違いを知る必要があります。宣言はコンパイラに "関数が存在する"ことを伝え、定義はコンパイラに "これは実装"を指示します。ヘッダーのみを含めては十分ではありません。リンクするときに、定義が必要です。 – kaylum