0

I am trying to built a C++ app with SDL. My plan was statically link the library so I don't have to deal with dlls. ( my previous question: Where do one start on deploying C++ code? ). And I am following the steps from here: How can i statically link standard library to my c++ program? )

I probably must somehow tell the compiler to where to find libraries but I don't know how to do it.

I tried g++ -B <path> -c -Wall main.cpp

But it still complains that it cannot find SDL.h (path above points to the folder that contains SDL.h).

I looked for -B option from help menu and probably using it wrong.

How do I tell the compiler where to find header files, and then to the linker, where to find...whatever it needs to find if it's necessary to do something related to libraries at this point?

( As you might have guessed, I have very limited knowledge about manually building projects and deploying, so any source is also appreicated. )

aulven
  • 521
  • 3
  • 14
  • How did you build your project before? – HolyBlackCat Apr 03 '20 at 22:38
  • Using IDEs, I am using Eclipse now. But I could't manage to do what I wanted with the IDE. I am trying to be able to send a folder to someone and them to be able to to just click on the exe and run the application. – aulven Apr 03 '20 at 22:41

1 Answers1

2

Compiling C/C++ code is quite a big topic, so I would definitely recommend you to also read a tutorial like this: https://www.geeksforgeeks.org/compiling-with-g-plus-plus/, and https://courses.cs.washington.edu/courses/cse373/99au/unix/g++.html.

But anyway:

This is a example project, which uses the library libFoo, and includes several header files from that library:

Myproject:
   - Main.cpp
   - ClassFoo.cpp
   - ClassFoo.hpp

A part of ClassFoo.cpp:

#include "AClassFooHeader.hpp"

ClassFoo::myFunc() {
   foo.execute(); // execute a random function in the libFoo library
}

Now let's say we want to compile this project, what command do we need to execute?

So the first part is compiling the project. Basically we will transform the source files into object files. After that we will link them together in a application (together with a library).

Let's say we run this command:

g++ -c Main.cpp ClassFoo.cpp

We will get a include error like: "can't find AClassFooHeader.hpp". Why is that? Well, the compiler searches on some predefined locations for the header files, but our library is not in a predefined location, so it can't find the header file. How can we solve this?

g++ -I/path/to/lib/folderHeader -c Main.cpp ClassFoo.cpp

So now we included the path to the file, so the compiler knows where to find the AClassFooHeader.hpp

But now we also need to link the objects together in a application. Without a library, this can be done with:

g++ -o application.exe Main.o ClassFoo.o

So we link the .o files in a application (called application.exe)

But now we also need to link our library. This can be done with:

g++ -o application.exe Main.o ClassFoo.o -L/path/to/lib/folderLibrary -lFoo

Where -L is the location where the library is stored, and -l is the name of the library minus lib, so for example libFoo -> -lFoo

This can also be done in a single step:

g++ -o application.exe -I/path/to/lib/folderHeader Main.cpp ClassFoo.cpp -L/path/to/lib/folderLibrary -lFoo
gummy
  • 123
  • 1
  • 6
  • I sincerely thank you for both the sources and detailed explanation. You don't know how much I was banging my head against the walls for hours just trying to find a source or at least a starting point. This is exactly what I was looking for. – aulven Apr 04 '20 at 00:14