24,860
社区成员




#include <iostream>
#include <string>
#include "testA.h"
#include "testB.h"
using namespace std;
int main()
{
testA();
testB();
return 0;
}
//testA.h
#ifndef TESTA_H
#define TESTA_H
void testA(void);
#endif
//testA.cpp
#include <iostream>
#include "testA.h"
#include "testC.h"
using namespace std;
void testA(void)
{
cout<<"A"<<endl;
testC();
}
//testB.h
#ifndef TESTB_H
#define TESTB_H
void testB(void);
#endif
//testB.cpp
#include <iostream>
#include "testB.h"
#include "testC.h"
using namespace std;
void testB(void)
{
cout<<"B"<<endl;
testC();
}
//testC.h
#ifndef TESTC_H
#define TESTC_H
void testC(void);
#endif
//testC.cpp
#include <iostream>
using namespace std;
void testC(void)
{
cout<<"C"<<endl;
}
main:main.cpp testA.o testB.o testC.o
g++ main.cpp testA.o testB.o testC.o -o main
rm *.o
testA.o:testA.cpp
g++ testA.cpp -c
testB.o:testB.cpp
g++ testB.cpp -c
testC.o:testC.cpp
g++ testC.cpp -c
g++ testC.cpp -c
g++ main.cpp testA.o testB.o testC.o -o main
testB.o: In function `testC()':
testB.cpp:(.text+0x0): multiple definition of `testC()'
testA.o:testA.cpp:(.text+0x0): first defined here
testC.o: In function `testC()':
testC.cpp:(.text+0x0): multiple definition of `testC()'
testA.o:testA.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [main] 错误 1
static void testC(void)
{
cout<<"C"<<endl;
}
void testC(void) { cout<<"C"<<endl; }
make
g++ testA.cpp -c
g++ testB.cpp -c
g++ testC.cpp -c
g++ main.cpp testA.o testB.o testC.o -o main
testA.o: In function `testA()':
testA.cpp:(.text+0x2b): undefined reference to `testC()'
testB.o: In function `testB()':
testB.cpp:(.text+0x2b): undefined reference to `testC()'
collect2: ld returned 1 exit status
make: *** [main] 错误 1
g++ testC.cpp -c
g++ main.cpp testA.o testB.o testC.o -o main
testB.o: In function `testC()':
testB.cpp:(.text+0x0): multiple definition of `testC()'
testA.o:testA.cpp:(.text+0x0): first defined here
collect2: ld returned 1 exit status
make: *** [main] 错误 1