Search code examples
c++makefilelibrariesgoogletestwindows-subsystem-for-linux

Error with gtest installation on home system


Im trying to setup my home computer the same way as the computers at my school so I can work on my assignments from here, but I can't for the life of me get gtest working correctly. I've run through the recommended install process and figured out that it needs the ".so" libraries to not throw 'pthread' not found errors.

Makefile:

PROJECT_DIR = Electra
PROGRAM_TEST = testProject

CXX = g++
CXXFLAGS = -std=c++11 -g -fprofile-arcs -ftest-coverage

LINKFLAGS = -lgtest

SRC_DIR = src

TEST_DIR = test

SRC_INCLUDE = include
INCLUDE = -I ${SRC_INCLUDE}

GCOV = gcov
LCOV = lcov
COVERAGE_RESULTS = results.coverage
COVERAGE_DIR = docs/code/coverage

STATIC_ANALYSIS = cppcheck

STYLE_CHECK = cpplint.py

DOXY_DIR = docs/code

#Targets
#
#.PHONY: all
#all: $(PROGRAM_TEST) memcheck coverage docs static style

#Temporary all target. use ^^^ this one once docs and coverage required
.PHONY: all
all: $(PROGRAM_TEST) memcheck static style

%.o: %.cpp
    $(CXX) $(CXXFLAGS) -c $< -o $@

.PHONY: clean
clean:
    rm -rf *~ $(SRC)/*.o $(TEST_DIR)/output/*.dat \
        *.gcov *.gcda *.gcno *.orig ???*/*.orig \
        *.bak ???*/*.bak $(PROGRAM_GAME) \
        ???*/*~ ???*/???*/*~ $(COVERAGE_RESULTS) \
        $(PROGRAM_TEST) $(MEMCHECK_RESULTS) $(COVERAGE_DIR)  \
        $(DOXY_DIR)/html obj bin

$(PROGRAM_TEST): $(TEST_DIR) $(SRC_DIR)
    $(CXX) $(CXXFLAGS) -o $(PROGRAM_TEST) $(INCLUDE) \
        $(TEST_DIR)/*.cpp $(SRC_DIR)/*.cpp $(LINKFLAGS)

tests: $(PROGRAM_TEST)
    $(PROGRAM_TEST)

memcheck: $(PROGRAM_TEST)
    valgrind --tool=memcheck --leak-check=yes $(PROGRAM_TEST)

fullmemcheck: $(PROGRAM_TEST)
    valgrind --tool=memcheck --leak-check=full $(PROGRAM_TEST)

coverage: $(PROGRAM_TEST)
    $(PROGRAM_TEST)
        # Determine code coverage
    $(LCOV) --capture --gcov-tool $(GCOV) --directory . --output-file $(COVERAGE_RESULTS)
        # Only show code coverage for the source code files (not library files)
    $(LCOV) --extract $(COVERAGE_RESULTS) */$(PROJECT_DIR)/$(SRC_DIR)/* -o $(COVERAGE_RESULTS)
        #Generate the HTML reports
    genhtml $(COVERAGE_RESULTS) --output-directory $(COVERAGE_DIR)
        #Remove all of the generated files from gcov
    rm -f *.gcda *.gcno

static: ${SRC_DIR} ${TEST_DIR}
    ${STATIC_ANALYSIS} --verbose --enable=all ${SRC_DIR} ${TEST_DIR} ${SRC_INCLUDE} --suppress=missingInclude

style: ${SRC_DIR} ${TEST_DIR} ${SRC_INCLUDE}
    ${STYLE_CHECK} $(SRC_INCLUDE)/* ${SRC_DIR}/* ${TEST_DIR}/*

#.PHONY: docs
#docs: ${SRC_INCLUDE}
#        doxygen $(DOXY_DIR)/doxyfile

Running "make tests" results in the following

g++ -std=c++11 -g -fprofile-arcs -ftest-coverage -o testProject -I include \
        test/*.cpp src/*.cpp -lgtest
testProject
make: testProject: Command not found
Makefile:53: recipe for target 'tests' failed
make: *** [tests] Error 127

Any idea as to why this wont work? Or how to even get started trying to resolve this? Its not a very detailed error. I don't want to change the Makefile, as it works for my school systems and this is a shared project.

My home system is running Windows 10, and im using the Ubuntu shell to run makefiles


Solution

  • In POSIX shells the current working directory is not searched by default. This is a safety measure that comes from POSIX's origins as a multi-user system: you don't want someone to be able to drop a program like ls in some directory and have unsuspecting people run it just by typing ls in that directory.

    Apparently in your school systems, someone has added the current working directory (.) to your PATH environment variable, while at home you do not have it added.

    Your makefile is wrong, the recipe should be:

    tests: $(PROGRAM_TEST)
            ./$(PROGRAM_TEST)
    

    to force the program from the current working directory to be run, instead of relying on the cwd appearing in the PATH (or running some other instance of testProgram that does happen to be on your PATH).

    This will work on all your systems.