Home > database >  Makefile Combining Object Creation With Linking
Makefile Combining Object Creation With Linking

Time:09-28

I am trying to follow and convert [this][1] Googletest tutorial to work in windows and use Make for building. I am also trying to add a more typical directory structure. [1]: https://www.eriksmistad.no/getting-started-with-google-test-on-ubuntu/

My directory structure:

D:.
│   makefile
│
├───bin
├───inc
│       whattotest.h
│
├───obj
├───src
│       whattotest.cpp
│
└───tests
        tests.cpp

whattotest.h

#pragma once

double squareRoot(const double a);

whattotest.cpp

#include <math.h>
#include "whattotest.h"
 
double squareRoot(const double a) {
    double b = sqrt(a);
    if(b != b) { // nan check
        return -1.0;
    }else{
        return sqrt(a);
    }
}

tests.cpp

#include "whattotest.h"
#include <gtest/gtest.h>
 
TEST(SquareRootTest, PositiveNos) { 
    ASSERT_EQ(6, squareRoot(36.0));
    ASSERT_EQ(18.0, squareRoot(324.0));
    ASSERT_EQ(25.4, squareRoot(645.16));
    ASSERT_EQ(0, squareRoot(0.0));
}
 
TEST(SquareRootTest, NegativeNos) {
    ASSERT_EQ(-1.0, squareRoot(-15.0));
    ASSERT_EQ(-1.0, squareRoot(-0.2));
}
 
int main(int argc, char **argv) {
    testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

makefile

CXX = g  
OBJ = obj
SRC = src
TESTS = tests
INC = inc
BIN = bin
LIBS= -lpthread
GTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\googletest\include\
LIBGTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\lib\libgtest.a

.PHONY : all
all: $(BIN)/runTests

$(BIN)/runTests: $(OBJ)/tests.o $(OBJ)/whattotest.o $(LIBGTEST)
    #g   -o runTests obj/tests.o obj/whattotest.o <path to libgtest.a> -pthread
    $(CXX) -o $@ $^ $(LIBS)
    
$(OBJ)/tests.o: $(TESTS)/tests.cpp
    #g   -I <path to gtest> -I inc -c tests.cpp -o tests.o
    $(CXX) -I $(GTEST) -I $(INC) -c $< -o $@

$(OBJ)/whattotest.o: $(SRC)/whattotest.cpp
    $(CXX) -I $(INC) -c $< -o $@
    
clean:
    -rm $(OBJ)/*.o
    -rm $(BIN)/*.exe

When I run this I get the following error:

g   -I D:\myWorkspace\Gtest\googletest-release-1.11.0\googletest\include LIBGTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\lib\libgtest.a -I inc -c tests/tests.cpp -o obj/tests.o
g  : warning: LIBGTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\lib\libgtest.a: linker input file unused because linking not done
g  : error: LIBGTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\lib\libgtest.a: linker input file not found: Invalid argument
make: *** [makefile:19: obj/tests.o] Error 1

The way I thought this would run would be for Make to see the Rule for runTests requires tests.o, which does not exist, then drop down and use the Rule for creating that object file. After that, return to the Rule for runTests and complete.

The output looks like Make is combining the two rules together into some kind of amalgamation with -c and .cpp files combined. Which I believe is causing this error to occur.

I am confused as to why Make is doing this.

CodePudding user response:

Check this:

GTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\googletest\include\
LIBGTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\lib\libgtest.a

By having a backslash at the end of the GTEST variable assignment you've continued that line to the next line, so GTEST contains the variable assignment on the next line as well (and LIBGTEST is not set).

You've basically written this:

GTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\googletest\include LIBGTEST=D:\myWorkspace\Gtest\googletest-release-1.11.0\lib\libgtest.a

You should not, in general, use backslashes in makefiles. You should use forward slashes for directory separators.

If you do want to use backslashes at the least you should not add them at the end of variable assignments.

  • Related