• 0

C++ Multiplatform Library?


Question

Hey guys I was hoping you could help me out here, I am a bit new to C++ and trying to find a way how I could create a multi-platform library or toolkit for both windows and linux?

I have base interface classes and 2 derived classes for each, one for linux and one for windows.

I have heard about putting pre-processor macros into the base-interface header which can then include the platform specific header.

The problem I am having is understanding how to put this all together into a single shared/dynamic library?

I mean do I compile the linux part and windows part seperately under each platform as dynamic libraries? which then generates a .dll for windows and a .so for linux... so I get 2 libraries?

A bit puzzled, any help is greatly appreciated

Link to comment
Share on other sites

5 answers to this question

Recommended Posts

  • 0
Hey guys I was hoping you could help me out here, I am a bit new to C++ and trying to find a way how I could create a multi-platform library or toolkit for both windows and linux?

I have base interface classes and 2 derived classes for each, one for linux and one for windows.

I have heard about putting pre-processor macros into the base-interface header which can then include the platform specific header.

Don't put platform-specific headers in the base class. There shouldn't be any reason for it.

The problem I am having is understanding how to put this all together into a single shared/dynamic library?

I mean do I compile the linux part and windows part seperately under each platform as dynamic libraries? which then generates a .dll for windows and a .so for linux... so I get 2 libraries?

Yes, you need to compile a .dll for windows binaries to link against and a .so for linux binaries. Of course, if it's your own project, you can forego the dynamic libraries and just compile it with your main program or use a static library.

Link to comment
Share on other sites

  • 0

Thanks for the advice, another quick question, windows has the PATH environment variable for finding your libraries .dll files but what about finding your header files?

With linux i know it's /usr/include but does windows have any default directory or a environment variable where I could add a directory location to the header files just like the PATH variable?

Link to comment
Share on other sites

  • 0
Thanks for the advice, another quick question, windows has the PATH environment variable for finding your libraries .dll files but what about finding your header files?

Maybe I'm wrong (I'm not a windows user), but PATH is for finding programs, not libraries. Libraries are found by searching the default locations (/Windows, /windows/system, etc) and the program's directory. For headers, it's dependent on your compiler and IDE. With visual studio, it's somewhere in your project settings. I can't help you in this space since I code on Linux and OSX, which are pretty much the same from my perspective.

With linux i know it's /usr/include but does windows have any default directory or a environment variable where I could add a directory location to the header files just like the PATH variable?

I don't think so, but some windows dev should give you a proper answer. As for Linux, don't put header files in /usr/include. That directory should be reserved for headers installed by your package manager. Anything you compile/install manually should be put in /usr/local/{bin,include,lib,...}. If you plan on distributing your library, your package should be flexible. Your code doesn't need to know where your headers are since that's something the compiler should take care of.

Link to comment
Share on other sites

  • 0
Maybe I'm wrong (I'm not a windows user), but PATH is for finding programs, not libraries. Libraries are found by searching the default locations (/Windows, /windows/system, etc) and the program's directory.

Windows does use the PATH environment variable when searching for DLLs. The order of precedence is listed in the documentation for LoadLibrary.

For headers, it's dependent on your compiler and IDE.

Yes. Visual Studio is just a wrapper for the compiler, which is a command-line tool. Microsoft's compilers look for headers in the current directory, in any directory explicitly passed via to the compiler via the command line or build script, or in the INCLUDE environment variable (which can be manually set, or if Visual Studio launches the compiler, is set by VS). And of course, this is all compiler-specific. If you're building using MingGW, Borland, or other non-Microsoft tools, this will vary.

Generally speaking, how to integrate a library into an existing project is something that should be left to the programmer. Programmers should know what they are doing and should have a better idea than you of how to best handle the logistics of incorporating a library into their code.

Link to comment
Share on other sites

  • 0

Thanks for the replies guys, I guess your right, I think i'm stressing too much on how to distribute it when programmers themselves could take care of this.

As for the linux part regarding using /usr/local/

I hear that some popular linux distros doesn't include /usr/local/ in their default search directory which was a bit strange.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.