Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6949857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T14:00:42+00:00 2026-05-27T14:00:42+00:00

I have a code which is compiled into a library (dll, static library and

  • 0

I have a code which is compiled into a library (dll, static library and so). I want the user of this library to use some struct to pass some data as parameters for the library function. I thought about declaring the struct in the API header file.

  • Is it safe to do so, considering compilation with different compilers, with respect to structure alignment or other things I didn’t think about?
  • Will it require the usage of the same compiler (and flags) for both the library and its user?

Few notes:

  1. I considered giving the user a pointer and set all the struct via functions in the library, but this will make the API really not comfortable to use.
  2. This question is about C, although it would be nice to know if there’s a difference in c++.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T14:00:43+00:00Added an answer on May 27, 2026 at 2:00 pm

    If it’s a regular/static library, the library and application should be compiled using the same compiler. There’re a few reasons for this that I can think of:

    1. Different compilers (as in different brands or compilers for different platforms) normally don’t understand each other’s object and library formats.
    2. You don’t want to compile different parts of the same program using different types (e.g. signed vs unsigned char), type sizes (e.g. long = 32 vs 64 bits), alignment and packing and probably some other things, all of which are allowed by the C standard to vary. Mixing and matching those things is usually a bad thing.

    You may, however, often use slightly different versions of the same compiler to compile the library and the application using it. Usually, it’s OK. Sometimes there’re changes that break the code, though.

    You may implement some “initialization” function in that header file (declared as static inline) that would ensure that types, type sizes, alignment and packing are the same as expected by the compiled library. The application using this library would have to call this function prior to using any other part of the library. If things aren’t the same as expected, the function must fail and cause program termination, possibly with some good textual description of the failure. This won’t solve completely the problem of having somewhat incompatible compilers, but it can prevent silent and mysterious malfunctions. Some things can be checked with the preprocessor’s #if and #ifdef directives and cause compilation errors with #error.

    In addition, structure packing problems can be relieved by inserting explicit padding bytes into structure declarations and forcing tight packing (by e.g. using #pragma pack, which is supported by many compilers). That way if type sizes are the same, it won’t matter what the default packing is.

    You can apply the same to DLLs as well, but you should really expect that the calling application has been compiled with a different compiler and not depend on the compilers being the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class library in C# which is compiled into a dll. Is
I have added some code which compiles cleanly and have just received this Windows
I have written some code that makes use of an open source library to
I have inherited a code-base which uses a compiled logging library. I cannot update
I have this code which compiles and works as expected: class Right {}; class
I have code which needs to do something like this There is a list
I have code which looks like: private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
I have some code which collects points (consed integers) from a loop which looks
I have have some code which adds new cells to a table and fills
I need to compile LGPL licensed code into a proprietary static iOS library. Since

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.