How to run C++ tasks on macOS
by Michael Nitsenko
Prerequisites
Setup
1) Create a new Xcode project from Command Line Tool template, fill all the required information about the project and pick C++ as the development language.
g++ main.cpp -o main $(pkg-config allegro-5 allegro_font-5 --libs --cflags) -lallegro -lallegro_main
2) Download homework template from CGLearn.
3) Locate main.cpp in the downloaded template (most likely "main.cpp" will be in the root folder or in "src" folder of the template).
4) Copy all its content and put it in the "main.cpp" file of the freshly created Xcode project overriding all the previous content of the Xcode's "main.cpp"
4.1) For Allegro-base homeworks, change main function definition to be
int main(int argc, char **argv) { // all the code that was inside // }
5) Check headers that are declared on top of the file.
5.1) For all the #include "some_file_name.h", locate corresponding some_file_name.h and some_file_name.cpp in the homework template and drag them to the project.
5.2) For all the #include <some_folder/some_header.h> that produce error:
a) try to find and add them in the Frameworks and Libraries section of the project settings
b) or find corresponding Homebrew formulas and install them via brew command in Terminal app.
brew install allegro
brew install glew
brew install glfw
brew install glm
6) For the Libs installed via Homebrew there is a need to find out two things:
- where these libs and headers are located
- what flags are required for them to compile
pkg-config <library_names> --libs --cflags
pkg-config allegro-5 allegro_font-5 --libs --cflags
pkg-config glew glfw glm --libs --cflags
7) Place all include paths into Headers Search Paths of the project, all the lib paths into Library Search Paths and all the flags into Other Linker Flags.
9) Any files that are loaded/read from the program by relative path should be placed relatively to the executable location. The location of the executable can be found as follows:
10) That's it - you are good to go with your homework, good luck!
Troubleshooting
Allegro wiki
Setting up Xcode for C projects
Undefined symbols errors
How do I use Allegro?
al_mangled_main() issue