You are not logged in.

#1 2014-11-18 20:14:02

chtis
Member
From: Vienna
Registered: 2014-10-20
Posts: 7

Writing a clean cross-platform library

Hello archers,

I want to ask a/some question(s) here, because I think some of you are better programmers than I am. I want to make a
cross-platform graphics library based on SDL2 (for creating simple 2D games). I know there is plenty of these,
but I do this for learning purposes.
I would like to learn, how to build a library for Linux and Windows.

1. Is it true, that libraries coming from packages are installed in /usr/lib/ and libraries coming from "make install"s should
be placed in /usr/local/lib ?

2. When I make a pakage for arch, and I put the library file into /usr/lib and the header files into /usr/header, did I forget
about something? And... are there big differences to other distros?

3. My library source consists of *.c files as well as *.h files. Is it good practice to write these (latter) header files so that
they can be deployed as the "real" header files, that application programmers will actually include in their projects, or
is it better to have a bunch of header files for the library (possibly containing declarations of functions that you don't want
to be called by application programmers) and a second bunch of header files that you then deploy on the users machine?

4. cmake, premake, configure script, Makefile...? wtf should I use?
I tried cmake, which seemed very blown up to me, very confusing and not easy-to-use (I thought it was developed to
be easy, though). I never got Makefiles to work properly. I tried premake, too, and I
was very pleased with it. It is not very common though, is it? I don't like to require people who want to examine my lib
to install many packages so that they can use it.

5. Structure... I mean, I would like to do development with a GitHub repo and I was wondering if there is a "standard"
way to structure your source code into folders. I mean, I'm planning to write a core lib (in C) and a C++ wrapper.
I was thinking of Makefile/premake/CMakeList/whatever, README, INSTALL, etc. in the root directory, then a folder
that contains the core lib, another folder that contains the C++ wrapper and a folder for all header files (which depends
on question 3 as well)...

I think you can see, I'm very confused...
Lastly, have you got any handy tips?

I would be happy to hear your opinions!

Offline

#2 2014-11-18 21:54:09

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Writing a clean cross-platform library

chtis wrote:

1. Is it true, that libraries coming from packages are installed in /usr/lib/ and libraries coming from "make install"s should
be placed in /usr/local/lib ?

2. When I make a pakage for arch, and I put the library file into /usr/lib and the header files into /usr/header, did I forget
about something? And... are there big differences to other distros?

This is completely entirely 100% unimportant. If you use GNU Autotools (or any similar software) then the person who installs / packages your library will have complete control over where to install it. smile

3. My library source consists of *.c files as well as *.h files. Is it good practice to write these (latter) header files so that
they can be deployed as the "real" header files, that application programmers will actually include in their projects, or
is it better to have a bunch of header files for the library (possibly containing declarations of functions that you don't want
to be called by application programmers) and a second bunch of header files that you then deploy on the users machine?

One set. Writing two just sounds tiring. tongue

4. cmake, premake, configure script, Makefile...? wtf should I use?

I use GNU Autotools because it's just so darn traditional. It has the steepest learning curve to anything I've ever used (aside from that $^#% FVWM...) but isn't too bad once I got it to work once. Let me know if you'd like an introduction to it.

5. Structure... I mean, I would like to do development with a GitHub repo and I was wondering if there is a "standard"
way to structure your source code into folders. I mean, I'm planning to write a core lib (in C) and a C++ wrapper.
I was thinking of Makefile/premake/CMakeList/whatever, README, INSTALL, etc. in the root directory, then a folder
that contains the core lib, another folder that contains the C++ wrapper and a folder for all header files (which depends
on question 3 as well)...

GNU Autotools will create the README, INSTALL, LICENSE, and so on files for you (as templates). Everything is pretty much up to you. I have a "src" directory for source code, you can put header files in "include", and that's pretty much it. The C++ wrapper files can be in a directory called "cpp".

I think you can see, I'm very confused...

You don't sound very confused. smile These questions are all the simple beginning steps. Did you write any code yet? Is it posted online? I use the Allegro game library, which is relevant to my interests. wink

(Sorry if I missed anything or misunderstood! I kind of typed in a hurry...)

Offline

#3 2014-11-19 15:42:12

chtis
Member
From: Vienna
Registered: 2014-10-20
Posts: 7

Re: Writing a clean cross-platform library

Thank you! Great post!
So I guess I'll have a look at autotools smile
If you know of good tutorials, please let me know smile

No I did not write any code, yet. In fact I never wrote a C/C++ library before,
only applications/small games and modules for another programming language.
That's why I had some questions

I had a look at Allegro some years ago, but somehow forgot about it and just
googled it now. It might be able to serve my interests as well...
okay, I have to rethink my idea...

Still, your post was very helpful! wink

Offline

#4 2014-11-19 16:11:02

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: Writing a clean cross-platform library

You're welcome. smile

chtis wrote:

If you know of good tutorials, please let me know smile

Sure, here is my "tutorial": https://bbs.archlinux.org/viewtopic.php … 2#p1255512

I had a look at Allegro some years ago, but somehow forgot about it and just googled it now.

It was just last week that I restarted work on one of the old game ideas I had. I was writing in in Python in order to better understand the language and to learn the Pyglet game library. But somehow I always end up coming back to ANSI C and Allegro. Oh well. Stick with what works and what you enjoy, that's what I say. tongue

EDIT: I just reread my little autotools "tutorial". Wow, I'm glad I wrote that. It's super simple to follow AND I've already completely forgotten how autotools works. big_smile

Last edited by drcouzelis (2014-11-19 16:13:51)

Offline

#5 2014-11-19 16:18:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 30,332
Website

Re: Writing a clean cross-platform library

I'll just chime in to second the recommendation of autotools.  I don't personally use them, and have measured criticism of them at times - but developing a cross-platform library is exactly the use-case for which GNU autotools will make things easier for everyone involved (you, and any users) - definitely use autotools.

FWIW, I've never heard of /usr/header, did you mean /usr/include?  That's where they should go - but in any case, autotools will help with that.

As for directory structure of the repo - if autotools documentation doesn't provide all the info needed, I'd also recommend checking out debian's packaging standards.  I say that as debian's standards are the most restrictive in that regard.  Having tried to deploy packages on a number of linux distros, most work fine with whatever structure I felt was useful for my own use - but not debian.  For debian I basically have to create a whole new base directory, copy things over, move them about ... it's a real pain.  Plan ahead for that.


"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman

Offline

#6 2014-11-19 16:34:45

progandy
Member
Registered: 2012-05-17
Posts: 5,280

Re: Writing a clean cross-platform library

Trilby wrote:

I'll just chime in to second the recommendation of autotools.  I don't personally use them, and have measured criticism of them at times - but developing a cross-platform library is exactly the use-case for which GNU autotools will make things easier for everyone involved (you, and any users) - definitely use autotools.

If you wish to use the same build environment on windows with visual c++ , then autotools is not the best choice. Better look into cmake or use visual studio project files for your windows build.


| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |

Offline

#7 2014-11-20 18:29:23

chtis
Member
From: Vienna
Registered: 2014-10-20
Posts: 7

Re: Writing a clean cross-platform library

Okay, thanks for your advices! smile

Offline

Board footer

Powered by FluxBB