Arvutiteaduse instituut
  1. Kursused
  2. 2020/21 sügis
  3. Arvutigraafika (MTAT.03.015)
EN
Logi sisse

Arvutigraafika 2020/21 sügis

  • Main
  • Lectures
  • Practices
  • Projects
  • Exam
  • Results
  • Links

How to run C++ tasks on macOS

by Michael Nitsenko

Prerequisites

  • Xcode
  • Homebrew

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.

Note: Xcode is not the only way, Terminal app can be used solely to compile c++ homework, however Xcode IDE is hard to underestimate for such tasks. Here is an example of command you will need to perform every time to compile basic allegro program:
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 // }
It is really important for main function to has all the parameters and return type as in the code above, because Allegro will look for exactly this function to provide allegro_main wrapping that provides required Allegro setup.

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.

For example, for "Cube Chopper" cpp task there is a need to add shader_utils 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.

For example, for "Hello, Allegro" task:
brew install allegro
To install allegro with homebrew.
For "Cube Chopper" task:
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

One option is to run pkg-config <library_names> --libs --cflags
For example, for "Hello, Allegro" task:
pkg-config allegro-5 allegro_font-5 --libs --cflags
For "Cube Chopper" task:
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.

Note: check headers of form #include <some_folder/some_header.h>, if they give "not found" error, then make sure that the folder structure of the lib corresponds to the folder structure mentioned in the header. For example, #include <GLEW/glew.h> header in "Cube Chopper" homework should be replaced with #include <GL/glew.h>.

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:

9.1) build the project successfully
9.2) open "Products" folder in the Xcode Project navigator
9.3) right click on the executable and select "Show in Finder" from the list of options that appeared
(after successful build executable in the project navigator should not be red)
For example, shader files should be moved to the executable folder in the "Cube Chopper" task.

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

  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Tartu Ülikooli arvutiteaduse instituudi kursuste läbiviimist toetavad järgmised programmid:
euroopa sotsiaalfondi logo