Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How can i link external c++ libraries #15

Closed
hunar1997 opened this issue Jan 5, 2019 · 8 comments
Closed

How can i link external c++ libraries #15

hunar1997 opened this issue Jan 5, 2019 · 8 comments

Comments

@hunar1997
Copy link
Contributor

hunar1997 commented Jan 5, 2019

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
Copy link

yamadapc commented Jan 5, 2019

@hunar1997
Copy link
Contributor Author

hunar1997 commented Jan 5, 2019

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
Copy link
Owner

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
Copy link
Owner

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
Copy link
Contributor Author

hunar1997 commented Jan 6, 2019

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
Copy link
Owner

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
Copy link
Owner

SanderMertens commented Jan 6, 2019

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
Copy link
Owner

Closing the issue as question is answered.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants