abc.c文件
#include "abc.h"
int abc()
{
return 10;
}
abc.h文件
int abc();
mymain.cpp文件
#include "abc.h"
int main()
{
abc();
return 0;
}
Makefile文件
CC=gcc -O2
CP=g++
mymain: mymain.o abc.o
$(CP) -o mymain mymain.o abc.o
mymain.o: mymain.cpp abc.h
$(CP) -c mymain.cpp abc.h
abc.o: abc.c abc.h
$(CC) -c abc.c abc.h
clean:
$(RM) *.o mymain
产量
g++ -c mymain.cpp abc.h
gcc -O2 -c abc.c abc.h
g++ -o mymain mymain.o abc.o
mymain.o: In function `main':
mymain.cpp:(.text+0x5): undefined reference to `abc()'
collect2: error: ld returned 1 exit status
make: *** [mymain] Error 1
为什么abc()是未定义的引用?
UPDATE
新的abc.h
extern "C" {
int abc();
}
错误
g++ -c mymain.cpp abc.h
gcc -O2 -c abc.c abc.h
In file included from abc.c:1:0:
abc.h:1:8: error: expected identifier or ‘(’ before string constant
extern "C" {
^
abc.h:1:8: error: expected identifier or ‘(’ before string constant
extern "C" {
^
make: *** [abc.o] Error 1