-2

I want to compile both the node.c,list.c and helper-functions.c with both the tests I built.

I'm sure I did not write my makefile as I should. Can you please check it out?

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

When I ran make, the output is:

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

There is definitly a reference to all those functions in node-test.cpp.

node-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);
}

the list-tests.cpp is similar to node-tests.cpp.

Stav Alfi
  • 13,139
  • 23
  • 99
  • 171
  • 1
    Possible duplicate of [What is an undefined reference/unresolved external symbol error and how do I fix it?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do-i-fix) – too honest for this site Apr 18 '17 at 14:14
  • Can you check my makefile if I wrote it ok? if Not pls refer to the related answer because there are 27 of them there. – Stav Alfi Apr 18 '17 at 14:18
  • We are not a free debuggingg service. The duplicate should answer your questrion. If it does not, state your **specific problem** and how it differs from the one in the dup! – too honest for this site Apr 18 '17 at 14:22
  • First of all I did. "I'm sure I did not write my makefile as I should. " Can you check it? – Stav Alfi Apr 18 '17 at 14:26

1 Answers1

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
Stav Alfi
  • 13,139
  • 23
  • 99
  • 171