2017-04-18 102 views
-2

我想用我構建的兩個測試編譯node.c,list.c和helper-functions.c。鏈接C和C++文件

我敢肯定,我沒有寫我的makefile,因爲我應該。你可以檢查一下嗎?

CFLAGS= -Wall -Wvla -Werror 
CXXFLAGS= -lgtest -lgtest_main -pthread 

all: 
    mkdir -p build 
    gcc -c node.c $(CFLAGS) -o build/node.o 
    gcc -c list.c $(CFLAGS) -o build/list.o 
    gcc -c tests/helper-functions.c -o build/helper-functions.o 
    g++ tests/node-tests.cpp $(CXXFLAGS) -o build/node-tests.o 
    g++ tests/list-tests.cpp $(CXXFLAGS) -o build/list-tests.o 
    g++ build/list.o build/node.o build/helper-functions.o build/node-tests.o build/list-tests.o -o build/tests.out 
    ./build/tests.out 

當我跑make,輸出爲:

mkdir -p build 
gcc -c node.c -Wall -Wvla -Werror -o build/node.o 
gcc -c list.c -Wall -Wvla -Werror -o build/list.o 
gcc -c tests/helper-functions.c -o build/helper-functions.o 
g++ tests/node-tests.cpp -lgtest -lgtest_main -pthread -o build/node-tests.o 
/tmp/ccFTgPVe.o: In function `CreateNode_FirstTest_Test::TestBody()': 
node-tests.cpp:(.text+0x21): undefined reference to `createNode' 
node-tests.cpp:(.text+0x31): undefined reference to `data' 
node-tests.cpp:(.text+0x108): undefined reference to `next' 
node-tests.cpp:(.text+0x1db): undefined reference to `freeNode' 
collect2: error: ld returned 1 exit status 
make: *** [all] Error 1 

有definitly在節點TEST.CPP所有這些函數的參考。

節點tests.cpp:

extern "C" 
{ 
    #include "../../../../headers/list.h" 
    #include "../../../../headers/node.h" 
    #include "helper-functions.h" 
    #include <assert.h> 
} 
#include "gtest/gtest.h" 
#include <iostream> 
#include <cstdlib> 
#include <string> 


// Node* CreateNode(void* data,const size_t dataLength,void* deepCopy(void*,const size_t)); 

TEST(CreateNode, FirstTest) { 

    int x=10; 
    Node* pos=createNode(&x,sizeof(x)); 
    EXPECT_TRUE(*(int*)data(pos)==x); 
    EXPECT_TRUE(next(pos)==NULL); 
    freeNode(pos); 
} 

的列表tests.cpp類似於節點tests.cpp。

+1

的可能的複製[什麼是未定義參考/解析的外部符號錯誤,以及如何解決呢?(http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference -unresolved-external-symbol-error-and-how-do-i-fix) – Olaf

+0

如果我寫了它,你可以檢查我的makefile嗎?如果不是請參考相關答案,因爲其中有27個。 –

+0

我們不是免費的調試服務。重複應回答你的問題。如果沒有,請說明**特定問題**,以及它與dup中的問題有何不同。 – Olaf

回答

0
CFLAGS= -Wall -Wvla -Werror 
CXXFLAGS= -lgtest -lgtest_main -pthread 

all: 
    mkdir -p build 
    gcc -c node.c $(CFLAGS) -o build/node.o 
    gcc -c list.c $(CFLAGS) -o build/list.o 
    gcc -c tests/helper-functions.c $(CFLAGS) -o build/helper-functions.o 
    g++ -c tests/node-tests.cpp -o build/node-tests.o //added -c, removed $(CXXFLAGS) 
    g++ -c tests/list-tests.cpp -o build/list-tests.o // added -c, $(CXXFLAGS) 
    g++ build/list.o build/node.o build/helper-functions.o build/node-tests.o build/list-tests.o $(CXXFLAGS) -o build/tests.out // added $(CXXFLAGS) 
    ./build/tests.out