Multiple entrance points in project

My question is related to C++ but it comes from Java actually.

When I was programming Java I had multiple classes, which were derived from a base “Test” class. Their purpose was to test things – run tests (unit tests/not unit tests).

In order to run a test, I had a public static void main in every class. So the matter of running such a test was to click run in Eclipse/Netbeans, whatever good IDE.

I know this question maybe IDE dependent, it actually boils down to a makefiles, but is it possible to maintain a similar structure in C++ IDEs?

Actually my question is: how do you deal with tests?

Do you put a huge main method with some switch/enum based statements to run the tests or make different build configurations for each test, or have different solution to this?

I know this question won’t get a straightforward answer, but I’m curious how to deal with it so I can choose something for myself.

3

The short answer is “you can’t do this in C or C++ as easily as you can in Java.”

Java lets you get away with having a main() method in every class because each class is its own namespace and can, in theory, be completely self-contained. When you invoke the JVM on a class, you’re telling it which namespace contains the main() you want it to run. So, for example, java Foo really means “run Foo.main().”

Languages that compile to native objects don’t work that way. Most systems have a bit of code that gets linked into executables and is responsible for doing a bit of pre-flight prep, calling main() and passing the int that it returns back to the operating system as an exit code. (On many Unixy systems, that code is in a file called crt0.o, or “C runtime zero.”) There’s no simple way to pass in an alternate main() function at runtime because the linker has to sew all of that up beforehand.

If you want to achieve something similar in C or C++, you have a couple of choices:

One way is to use the preprocessor to subvert the usual things main() does and call a method directly if a symbol is defined:

class Foo {
  public:
    static void test() { // Your Test Here }
};

int main() {
#ifdef TEST_CLASS
  TEST_CLASS ## ::test();
#else
  // Do whatever main() usually does
#endif
}

Then, to make this work, you’d have to have your IDE define TEST_CLASS to the name of the class whose test you’d like to run, recompile and run.

The other way is to compile all of your objects into a shared library and add exported-to-C symbols to run your tests. Then you’d need a small program that uses the dynamic loader to fish out the symbol for the test you want to run and call it. How you’d do this is very system-dependent and has no basis in a standard, so I’m not going to recommend it or elaborate on how it’s done.

1

You already have an accepted answer, but I think the answer is incomplete (especially since it said you can’t define multiple application entry points as easily as java).

To implement multiple entry points into the same code/module with C++, you do the following:

  • create a new cpp file for each entry point (i.e. main-app.cpp, io-tests.cpp, db-tests.cpp, network-tests.cpp, etc).

  • each file should define it’s own main function.

  • each file should have a separate binary target in your build system (make file?).

  • build and run targets as needed.

Basically it’s as easy as writing multiple main functions, each in their own file.

This solution works best if your main() function (in the example above, the one defined in “main-app.cpp”) is as thin as possible.

A good implementation would look similar to this:

main-app.cpp:

#include <application.hpp>

int main(int, char**) {
    application app(std::cin, std::cout); // run application with std i/o
    return app.run();
}

db-tests.cpp:

#include <database.hpp>    // tested class
#include <unit-tests.hpp>  // unit testing fw

int main(int, char**) {
    database_test_suite suite;
    return suite.run();
}

You would have no target that compiles both of these files together (so you would not have any linker error with multiple definitions for main).

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật