0

How to set-up Visual Sutio Code to compile an executable from several .cpp files? I have following directory structure:

src
test
build

where in "src" I have some .cpp files with a program and in "test" I have a main.cpp file with

#include <gtest/gtest.h>
int main(int argc, char *argv[]) {
    ::testing::InitGoogleTest(&argc, argv);
    return RUN_ALL_TESTS();
}

and several.cpp file with written tests.

How to compile it so that all .cpp will be included (linked?) and tests executed?

Right now I got stuck at compiling main.cpp and of course the result is

[==========] Running 0 tests from 0 test suites.
[==========] 0 tests from 0 test suites ran. (0 ms total)
[  PASSED  ] 0 tests.

I need this to be simple and for training purposes of google tests, never been dealing with make makefiles, CMake etc.

  • 2
    Do you know how the compilation process in C and C++ works? That would be a good start to learn about compilers and then makefiles, because you will need it very often – RoQuOTriX Aug 26 '20 at 14:08
  • Start by reading the documentation of [GCC](http://gcc.gnu.org/). Then read the documentation of [GNU make](https://www.gnu.org/software/make/). Read also a good [C++ programming book](https://stroustrup.com/programming.html) and [this C++ reference](https://en.cppreference.com/w/cpp). Of course read the documentation of [Visual Studio Code](https://code.visualstudio.com/) – Basile Starynkevitch Aug 26 '20 at 14:09
  • 1
    you dont need CMake first. Imho the best way to learn is to use plain command line. Once you know what it takes you can switch to makefiles and/or fancy build-buttons on IDEs – 463035818_is_not_an_ai Aug 26 '20 at 14:16
  • Maybe this can help: https://stackoverflow.com/questions/7443970/separate-test-cases-across-multiple-files-in-google-test ? – vahancho Aug 26 '20 at 14:19
  • https://code.visualstudio.com/docs/cpp/cmake-linux – bolov Aug 26 '20 at 14:35

1 Answers1

0

Thanks for all hints. I'm in "re-education" process after very long break (updating my knowledge to the latest C++ standards and practices/rules).

I did a workaround just by creating executable file with g++ command and all filepaths - worked fine, but when it comes to more complex examples I'm trainig with, it turns to be very inconvenient (even if it's only for training of gtest, which now I need to focus on).

I tried with make unfortunately stumbled upon problem of header files now being included - I don't understand why it happens while with g++ command directly it worked fine. Can you give some hint/tip ?

Here is makefile:

# Makefile
SOURCES=$(wildcard *.cpp */*.cpp */*/*.cpp)
DEPS=$(wildcard *.hpp */*.hpp */*/*.hpp)

main:
    g++ $(SOURCES) -lgtest -o $@ -pthread

Directory tree:

├── build
│
├── src
│   ├── Message.cpp
│   ├── Message.hpp
│   ├── SendMessage.cpp
│   ├── SendMessage.hpp
│   └── Transport
│       ├── AbstractTransport.cpp
│       ├── AbstractTransport.hpp
│       ├── EmailTransport.cpp
│       ├── EmailTransport.hpp
│       ├── SlackTransport.cpp
│       └── SlackTransport.hpp
├── tests
│   ├── Message
│   │   ├── EmailTransportTest.cpp
│   │   ├── Message.cpp
│   │   ├── SendMessage.cpp
│   │   └── SlackTransportTest.cpp
│   └── main.cpp
└──  makefile