Thread: link with C runtime library

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    link with C runtime library

    Hello everyone,


    I am studying the manual for DLLMain, it is mentioned,

    http://msdn2.microsoft.com/en-us/library/ms682583.aspx

    --------------------
    If your DLL is linked with the C run-time library (CRT), the entry point provided by the CRT calls the constructors and destructors for global and static C++ objects. Therefore, these restrictions for DllMain also apply to constructors and destructors and any code that is called from them.
    --------------------

    My questions are,

    1. How could I know if a project whether DLL or EXE or static lib is linked with C Runtime library?

    2. Why we need C Runtime library in a C++ project? We need to call some legacy C functions like printf other than pure C++ functions std::cout?

    3. If we use default entry point in DLL (DllMain), does it mean C Runtime Library is dynamically linked?


    thanks in advance,
    George

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    1) If you don't have the "Omit default libraries" option checked, you're linking against the CRT.

    2) MS iostreams are implemented in terms of the C file API. new might be implemented in terms of malloc. std::copy and std::reverse_copy might be implemented in terms of memmove for some types. std::fill might be implemented in terms of memset for some types. And there's still a chance that you actually want to call some of these.

    3) No, that's completely unrelated.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee!


    1.

    Quote Originally Posted by CornedBee View Post
    1) If you don't have the "Omit default libraries" option checked, you're linking against the CRT.
    Do you mean Linker --> Input --> Ignore All Default Libraries? (/NODEFAULTLIB)?

    Seems /MT, /MTd, /MD and /MDd are all different CRTs? What are the differences?

    2.

    Quote Originally Posted by CornedBee View Post
    2) MS iostreams are implemented in terms of the C file API. new might be implemented in terms of malloc. std::copy and std::reverse_copy might be implemented in terms of memmove for some types. std::fill might be implemented in terms of memset for some types. And there's still a chance that you actually want to call some of these.
    I think you mean we can not separate C and C++ libraries and they are mixed in use. Right? :-)


    regards,
    George

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    Seems /MT, /MTd, /MD and /MDd are all different CRTs? What are the differences?
    If you read the documentation, you would find:
    Multi-Threaded Release
    Multi-Threaded Debug
    Single-Threaded Release
    Single-Threaded Debug

    2. I think you mean we can not separate C and C++ libraries and they are mixed in use. Right? :-)
    I think it's safe to assume that it's not safe to only link with the C++ libraries even though you use C because the implementation of C++ is not required to not call the C runtime, like Microsoft's implementation does.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    1.

    Quote Originally Posted by Elysia View Post
    If you read the documentation, you would find:
    Multi-Threaded Release
    Multi-Threaded Debug
    Single-Threaded Release
    Single-Threaded Debug
    What I mean is something beyond the document itself. Multi-Threaded Release or Multi-Threaded Debug means static linking to CRT. And Single-Threaded DLL Release or Single-Threaded DLL Debug means dynamic linking to CRT. Right?

    2.

    Quote Originally Posted by Elysia View Post
    I think it's safe to assume that it's not safe to only link with the C++ libraries even though you use C because the implementation of C++ is not required to not call the C runtime, like Microsoft's implementation does.
    I am confused aftre reading your above statement. What is your real point (mixing it is safe and it is not safe makes me confused)? Could you explain in some other words please?


    regards,
    George

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by George2 View Post
    What I mean is something beyond the document itself. Multi-Threaded Release or Multi-Threaded Debug means static linking to CRT. And Single-Threaded DLL Release or Single-Threaded DLL Debug means dynamic linking to CRT. Right?
    No, of course not. As the name implies, it's Debug and Release and one is intended for multi-threaded applications and one is not. Nothing to do with static vs. dynamic linking.

    I am confused aftre reading your above statement. What is your real point (mixing it is safe and it is not safe makes me confused)? Could you explain in some other words please?
    Microsoft's C++ implementation calls its C implementation, thus needing the C runtime library. The standard does not forbid this behavior so it's unsafe to assume that the C++ implementation won't call the C implementation. Therefore you need to link with both libraries.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi Elysia,


    Here are some links describing the options in more details. You can see it deals with dynamic or static linking to CRT library. You can also try to make a build and see the sizes are different significantly.

    http://support.microsoft.com/kb/140584/en-us

    http://msdn2.microsoft.com/en-us/library/2kzt1wy3.aspx

    Agree?

    Quote Originally Posted by Elysia View Post
    No, of course not. As the name implies, it's Debug and Release and one is intended for multi-threaded applications and one is not. Nothing to do with static vs. dynamic linking.

    regards,
    George

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    There's 6 options, but the 4 Elysia listed are all static. The last two are the debug and release DLL versions, which are multi-threading-safe. There are no single-threaded versions of the DLL.

    The whole thing is an MS quirk. Other compilers might or might not provide separate versions of the CRT, and they might use the normal linking options for the decision.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks CornedBee,


    So /MT for static link to CRT, and /MD for dynamic link to CRT. /MTd and /MDd are related debug version, right?

    http://msdn2.microsoft.com/en-us/library/abx4dbyh.aspx

    Quote Originally Posted by CornedBee View Post
    There's 6 options, but the 4 Elysia listed are all static. The last two are the debug and release DLL versions, which are multi-threading-safe. There are no single-threaded versions of the DLL.

    The whole thing is an MS quirk. Other compilers might or might not provide separate versions of the CRT, and they might use the normal linking options for the decision.

    regards,
    George

  10. #10
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Probably. RTFM.

    I mean, the table under the link spells it out perfectly. Why do you need my confirmation? It's not like I know better than MSDN. (Aside from the broken docs of ReadFile, that is.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Hi CornedBee, RTFM means?


    The MSDN does not mentioned explicitly, so I come here for your confirmation. :-)


    Quote Originally Posted by CornedBee View Post
    Probably. RTFM.

    I mean, the table under the link spells it out perfectly. Why do you need my confirmation? It's not like I know better than MSDN. (Aside from the broken docs of ReadFile, that is.)

    regards,
    George

  12. #12
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    An acronym meaning you should read the manual. It's used when whatever is being asked is believed to be easily answered by simply looking at the available documentation.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  13. #13
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks Mario,


    I assume you agree with my points in post #9.

    Quote Originally Posted by Mario F. View Post
    An acronym meaning you should read the manual. It's used when whatever is being asked is believed to be easily answered by simply looking at the available documentation.

    regards,
    George

  14. #14
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    That's irrelevant. I'm only here to answer the RTFM question. Which in itself answers the one point and one question you make in post 9.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The MSDN does not mentioned explicitly
    Huh? Is your browser broken or something? Read the "Characteristics" and "Option" columns in the table on the very page you linked to.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Library Wrapper
    By cusavior in forum C Programming
    Replies: 3
    Last Post: 03-25-2008, 10:27 AM
  2. Link Error
    By Luigi in forum C++ Programming
    Replies: 1
    Last Post: 04-12-2004, 07:12 PM
  3. A runtime library error on this line???
    By Unimatrix_001 in forum C++ Programming
    Replies: 2
    Last Post: 09-04-2003, 03:42 PM
  4. link error: with Boost date_time library
    By Hotman_x in forum C++ Programming
    Replies: 0
    Last Post: 01-14-2003, 07:54 AM
  5. .lib vs .h vs .dll
    By Shadow12345 in forum C++ Programming
    Replies: 13
    Last Post: 01-01-2003, 05:29 AM