A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://gammasoft71.github.io/xtd/docs/documentation/guides/xtd.core/entry_point/main_and_startup below:

main function and startup_ keyword

main function and startup_ keyword main function

A program shall contain a global function named main, which is the designated start of the program in hosted environment.

It shall have one of the following forms:

See Main function for more information.

main function arguments

You can get command line arguments event if you use mainfunction without argument.

The xtd::environment class grabs and keeps for you the command line arguments :

xtd::environment::command_line and xtd::environment::get_command_line_args usage
#include <xtd/xtd>

using namespace xtd;

auto main() -> int {

console::write_line(environment::command_line());

console::write_line();


for (auto arg : environment::get_command_line_args())
console::write_line(arg);
}

As you can see, even if the main function is called without arguments and if one two "three four" five are entered on the command line, you can still retrieve them.

startup::safe_run methods

xtd introduce the xtd::startup::safe_run methods which allows to have a main static method in any class with different parameters with or without return value.

startup::safe_run methods and exceptions

Even ifxtd::startup::safe_run method catch exceptions, it's preferable that you catch yourself exception. Indeed startup_ generate a generic fallback message to the output console for a console application and a generic falbback xtd::forms::dialog_exception for a GUI application.

Your code should look like this :

#include <xtd/xtd>

using namespace xtd;

namespace examples {
class program {
public:
static auto main() -> void {
try {

} catch(const std::exception& e) {

}
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}

See try-block for more information.

startup::safe_run methods usage
#include <xtd/xtd>

using namespace xtd;

namespace examples {
class program {
public:
static auto main() -> void {

for (auto arg : environment::get_command_line_args())
console::write_line(arg);


environment::exit_code(42);
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}
#include <xtd/xtd>

using namespace xtd;

namespace examples {
class program {
public:
static auto main() -> int {

for (auto arg : environment::get_command_line_args())
console::write_line(arg);

return 42;
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static void main(const argument_collection& args) {

for (auto arg : args)
console::write_line(arg);


environment::exit_code(42);
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static int main(const argument_collection& args) {

for (auto arg : args)
console::write_line(arg);

return 42;
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static auto main(int argc, char* argv[]) -> void {

for (auto arg : {argv, argv + argc})
console::write_line(arg);


environment::exit_code(42);
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static auto main(int argc, char* argv[]) -> int {

for (auto arg : {argv, argv + argc})
console::write_line(arg);

return 42;
}
};
}

auto main() -> int {
return xtd::startup::safe_run(examples::program::main);
}
startup_ keyword

xtd introduces the keyword startup_ which allows to have a main static method in any class with different parameters with or without return value.

Behind this keyword there is a main global function that call main static method in the specified class parameter with try and catch.

startup_ definition
#define startup_(main_method) \
auto main(int argc, char* argv[]) -> int {\
return xtd::startup::safe_run(main_method, argc, argv);\
}\
intptr_t __opaque_sftews__ = 0
startup_ and exceptions

Even if startup_ keyword catch exceptions, it's preferable that you catch yourself exception. Indeed startup_ generate a generic fallback message to the output console for a console application and a generic falbback xtd::forms::dialog_exception for a GUI application.

Your code should look like this :

#include <xtd/xtd>

using namespace xtd;

namespace examples {
class program {
public:
static auto main() -> void {
try {

} catch(const std::exception& e) {

}
}
};
}

startup_(examples::program::main);

See try-block for more information.

statup_ keyword usage
#include <xtd/xtd>

using namespace xtd;

namespace examples {
class program {
public:
static auto main() -> void {

for (auto arg : environment::get_command_line_args())
console::write_line(arg);


environment::exit_code(42);
}
};
}

startup_(examples::program::main);
#include <xtd/xtd>

using namespace xtd;

namespace examples {
class program {
public:
static auto main() -> int {

for (auto arg : environment::get_command_line_args())
console::write_line(arg);

return 42;
}
};
}

startup_(examples::program::main);
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static void main(const argument_collection& args) {

for (auto arg : args)
console::write_line(arg);


environment::exit_code(42);
}
};
}

startup_(examples::program::main);
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static int main(const argument_collection& args) {

for (auto arg : args)
console::write_line(arg);

return 42;
}
};
}

startup_(examples::program::main);
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static auto main(int argc, char* argv[]) -> void {

for (auto arg : {argv, argv + argc})
console::write_line(arg);


environment::exit_code(42);
}
};
}

startup_(examples::program::main);
#include <xtd/xtd>

using namespace std;
using namespace xtd;

namespace examples {
class program {
public:
static auto main(int argc, char* argv[]) -> int {

for (auto arg : {argv, argv + argc})
console::write_line(arg);

return 42;
}
};
}

startup_(examples::program::main);
Windows main definitions

In Windows, there are different definitions for main in addition to the c++ standard :

main definitions in the standard c++ :

main definitions specific Windows :

See Using wmain and WinMain: The Application Entry Point for more information

For xtd portability, the /ENTRY:mainCRTStartup flag is added automatically when you add the xtd library in your CMakeLists.txt to the linker flags. So even for a GUI application you can call the main functions of the c++ standard in the Windows development environment.

If you are not using CMake for your project creation, it is advisable to add this flag manually.

See also

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4