SanderMertens / bake

Bake, A build system for building, testing and running C & C++ projects
GNU General Public License v3.0
685 stars 55 forks source link

How can i link external c++ libraries #15

Closed hunar1997 closed 5 years ago

hunar1997 commented 5 years ago

I just found this project on my news feed and it looks amazing

but while reading the README i didn't understand how to link to an external library

for example i googled an SFML helloworld and it includes the library like this
(#include <SFML/Window.hpp>)

then links it like this
(g++ -o sfml_hello sfml_hello.cpp -lsfml-graphics -lsfml-window -lsfml-system)

so what is the equivalent of (-lsome-library) in bake json file :D

This is the helloworld example:
gist.github.com/emersonmx/31004a311efb2876d187

Maybe you can make a working example of this code in the examples/cpp folder :D

yamadapc commented 5 years ago

Maybe https://github.com/SanderMertens/bake#can-i-link-with-non-bake-libraries

hunar1997 commented 5 years ago

Ok, i actually saw that but it didn't work, then i found that this is the problem:

{
    "id": "sfml",
    "type": "application",
    "value": {
        "language": "cpp"
    },
    "lang.c":{
    "lib":["sfml-graphics", "sfml-window", "sfml-system","stdc++"]
    }
}

I removed the "value": {"language": "cpp"} section and it works now .. but why? it is a C++ application not C? and why lang.c instead of lang.cpp?

SanderMertens commented 5 years ago

Ah, I can see why this is a bit confusing. I just added an example to the C++ folder. The correct configuration is:

{
    "id": "sfml",
    "type": "application",
    "value": {
        "language": "cpp"
    },
    "lang.cpp":{
    "lib":["sfml-graphics", "sfml-window", "sfml-system","stdc++"]
    }
}

Bake's configuration is composed out of generic configuration (id, type, value) and driver-specific configuration (everything else). In this example, everything under "lang.cpp" is configuration for the C++ driver.

Bake automatically loads the correct language driver based on the "language" attribute, which by default is C. In your example, you selected C++ as the language, but you provided configuration for the C driver, which is why it wasn't picking up the settings.

When you removed the "language": "cpp" attribute, the project became a C project, and bake correctly loaded the configuration in "lang.c" which is why it worked.

In practice, the C and C++ drivers are almost the same (C++ inherits the definitions from C) so most configuration settings apply to both. If you would like to know more about driver configuration, see the READMEs in the driver projects:

C: https://github.com/SanderMertens/bake/tree/master/drivers/lang/c C++: https://github.com/SanderMertens/bake/tree/master/drivers/lang/cpp

SanderMertens commented 5 years ago

Btw, here is a trick that, based on your example, you may find useful. You can encapsulate the SFML configuration, by doing this:

{
    "id": "sfml",
    "type": "package",
    "value": {
        "language": "none"
    },
    "dependee": {
        "lang.cpp": {
        "lib":["sfml-graphics", "sfml-window", "sfml-system","stdc++"]
        }
    }
}

Then, in your application project, you can simply do this:

{
    "id": "my_sfml_app",
    "type": "application",
    "value": {
        "language": "c++",
        "use": ["sfml"]
    }
}

Note I changed the name of the application, so that it doesn't clash with the SFML package.

hunar1997 commented 5 years ago

Thanks :D
will this project include those library configurations out of the box? like CMake does it with find_package(SFML 2 REQUIRED) or every user should write one of its own

SanderMertens commented 5 years ago

That is a good question. It would be nice if bake could provide configurations for well-known libraries out of the box. There could be a folder in the repository called libraries in which these configurations can be stored. During the bake setup, these libraries will then automatically be made available for user projects.

It would be great if you could upload the sfml configuration, once it is in place

SanderMertens commented 5 years ago

I just implemented the above functionality. Any projects you add to the location below will be automatically installed during bake setup:

https://github.com/SanderMertens/bake/tree/master/libraries

See the SDL2 template for an example.

SanderMertens commented 5 years ago

Closing the issue as question is answered.