Replacing MSVC CRT function with private implementationIs there replacement for cat on WindowsWindows malloc replacement (e.g., tcmalloc) and dynamic crt linkingLink a static library to a DLLSolving the multiple CRT problemCRT, do we still need to redistribute it?Any up-to-date references for replacing the MS CRT?Linking a static boost build into a static library under VS2012 & Win32/64What is the _SYSCRT macro for in MSVC C++ CRT?Prevent my project from calling the __CxxFrameHandler3 (CRT function)

What is the most remote airport from the center of the city it supposedly serves?

What is the limiting factor for a CAN bus to exceed 1Mbps bandwidth?

Did we get closer to another plane than we were supposed to, or was the pilot just protecting our delicate sensibilities?

Write to EXCEL from SQL DB using VBA script

Selecting a secure PIN for building access

Survey Confirmation - Emphasize the question or the answer?

Does hiding behind 5-ft-wide cover give full cover?

Power LED from 3.3V Power Pin without Resistor

How can I fairly adjudicate the effects of height differences on ranged attacks?

Is it cheaper to drop cargo than to land it?

Packet sniffer for MacOS Mojave and above

How does NAND gate work? (Very basic question)

My ID is expired, can I fly to the Bahamas with my passport

When do aircrafts become solarcrafts?

What happens if I start too many background jobs?

How to assert on pagereference where the endpoint of pagereference is predefined

How did Arya get back her dagger from Sansa?

Junior developer struggles: how to communicate with management?

How to scale a verbatim environment on a minipage?

I caught several of my students plagiarizing. Could it be my fault as a teacher?

If Melisandre foresaw another character closing blue eyes, why did she follow Stannis?

How to get SEEK accessing converted ID via view

How do you center multiple equations that have multiple steps?

Why is Thanos so tough at the beginning of "Avengers: Endgame"?



Replacing MSVC CRT function with private implementation


Is there replacement for cat on WindowsWindows malloc replacement (e.g., tcmalloc) and dynamic crt linkingLink a static library to a DLLSolving the multiple CRT problemCRT, do we still need to redistribute it?Any up-to-date references for replacing the MS CRT?Linking a static boost build into a static library under VS2012 & Win32/64What is the _SYSCRT macro for in MSVC C++ CRT?Prevent my project from calling the __CxxFrameHandler3 (CRT function)






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








2















I have an embedded IoT project that I like to first develop in part by using PC tools such as VisualStudio. My embedded project has only a flash memory for a file system, and I'd like to redirect fopen fread etc. to my own private implementation on Windows. But what I'm encountering is an inability to have my private CRT library take precedence over the built-in CRT (e.g., built-in behavior driven by /MD compiler switch).



I have a simple three project solution.



Project 1 is a test executable. It has a one line main:



int main()

test();



Project 2 and 3 are static libraries. Project 2 has:



#include <string.h>
#include <stdio.h>

void test()

printf("%sn", strchr("x", 'x'));



Project 3 has:



char * strchr(const char * s, int c) // exact signature of MSVC

return "overridden";



I expect output to be overridden but instead it is



x


But if I add this to Project 1:



printf("%sn", strchr("y", 'y'));


The output will be



overridden
overridden


First one from test() in library, second from executable main() directly.



Any suggestions?










share|improve this question
























  • What have you tried that does not work? Knowing that will save anyone from suggesting things you have already tried, or enable then to see where you might be going wrong if you approach is nearly correct.

    – Clifford
    Mar 22 at 20:29











  • I am not sure how having "only a flash memory for a file system" precludes the use of the standard file system I/O on the test system - that is rather the purpose of the stdio abstraction. A more usual requirement is to emulate an embedded non-volatile persistent storage API using the test environment's file-system. Unless of course it is teh file system itself you are testing.

    – Clifford
    Mar 22 at 20:37











  • Agreed, it uses a special CRT on the embedded device and is the point of using fopen etc. But development tools for the embedded device are sub-par. I like to have the richness of PC tools, such as nice debugging, code coverage, etc., that the embedded device does not offer. So what I need to do is emulate or mock embedded CRT on Windows, but, it's falling into actual Windows CRT instead.

    – jws
    Mar 22 at 20:40











  • If you are doing cross platform compiling and want to test an embedded program on a PC, you will certainly need some conditional preprocessor magic. You are basically writing your own HAL, and including <stdio.h> directly in your files means your are skipping the abstraction layer and going straight for the implementation. So start by creating your own header which will do the necessary wiring, and avoid using standard headers which access hardware directly (like <stdio.h>). It's likely you will want to avoid <stdlib.h> and anything related to malloc too.

    – Groo
    Mar 25 at 8:58












  • @Groo I am able to redirect CRT of a third party lib, which is proprietary and doesn't come with source. It's not necessary to create an entire abstraction layer. (I will give you this - the link solution isn't pretty.)

    – jws
    Mar 25 at 20:17

















2















I have an embedded IoT project that I like to first develop in part by using PC tools such as VisualStudio. My embedded project has only a flash memory for a file system, and I'd like to redirect fopen fread etc. to my own private implementation on Windows. But what I'm encountering is an inability to have my private CRT library take precedence over the built-in CRT (e.g., built-in behavior driven by /MD compiler switch).



I have a simple three project solution.



Project 1 is a test executable. It has a one line main:



int main()

test();



Project 2 and 3 are static libraries. Project 2 has:



#include <string.h>
#include <stdio.h>

void test()

printf("%sn", strchr("x", 'x'));



Project 3 has:



char * strchr(const char * s, int c) // exact signature of MSVC

return "overridden";



I expect output to be overridden but instead it is



x


But if I add this to Project 1:



printf("%sn", strchr("y", 'y'));


The output will be



overridden
overridden


First one from test() in library, second from executable main() directly.



Any suggestions?










share|improve this question
























  • What have you tried that does not work? Knowing that will save anyone from suggesting things you have already tried, or enable then to see where you might be going wrong if you approach is nearly correct.

    – Clifford
    Mar 22 at 20:29











  • I am not sure how having "only a flash memory for a file system" precludes the use of the standard file system I/O on the test system - that is rather the purpose of the stdio abstraction. A more usual requirement is to emulate an embedded non-volatile persistent storage API using the test environment's file-system. Unless of course it is teh file system itself you are testing.

    – Clifford
    Mar 22 at 20:37











  • Agreed, it uses a special CRT on the embedded device and is the point of using fopen etc. But development tools for the embedded device are sub-par. I like to have the richness of PC tools, such as nice debugging, code coverage, etc., that the embedded device does not offer. So what I need to do is emulate or mock embedded CRT on Windows, but, it's falling into actual Windows CRT instead.

    – jws
    Mar 22 at 20:40











  • If you are doing cross platform compiling and want to test an embedded program on a PC, you will certainly need some conditional preprocessor magic. You are basically writing your own HAL, and including <stdio.h> directly in your files means your are skipping the abstraction layer and going straight for the implementation. So start by creating your own header which will do the necessary wiring, and avoid using standard headers which access hardware directly (like <stdio.h>). It's likely you will want to avoid <stdlib.h> and anything related to malloc too.

    – Groo
    Mar 25 at 8:58












  • @Groo I am able to redirect CRT of a third party lib, which is proprietary and doesn't come with source. It's not necessary to create an entire abstraction layer. (I will give you this - the link solution isn't pretty.)

    – jws
    Mar 25 at 20:17













2












2








2








I have an embedded IoT project that I like to first develop in part by using PC tools such as VisualStudio. My embedded project has only a flash memory for a file system, and I'd like to redirect fopen fread etc. to my own private implementation on Windows. But what I'm encountering is an inability to have my private CRT library take precedence over the built-in CRT (e.g., built-in behavior driven by /MD compiler switch).



I have a simple three project solution.



Project 1 is a test executable. It has a one line main:



int main()

test();



Project 2 and 3 are static libraries. Project 2 has:



#include <string.h>
#include <stdio.h>

void test()

printf("%sn", strchr("x", 'x'));



Project 3 has:



char * strchr(const char * s, int c) // exact signature of MSVC

return "overridden";



I expect output to be overridden but instead it is



x


But if I add this to Project 1:



printf("%sn", strchr("y", 'y'));


The output will be



overridden
overridden


First one from test() in library, second from executable main() directly.



Any suggestions?










share|improve this question
















I have an embedded IoT project that I like to first develop in part by using PC tools such as VisualStudio. My embedded project has only a flash memory for a file system, and I'd like to redirect fopen fread etc. to my own private implementation on Windows. But what I'm encountering is an inability to have my private CRT library take precedence over the built-in CRT (e.g., built-in behavior driven by /MD compiler switch).



I have a simple three project solution.



Project 1 is a test executable. It has a one line main:



int main()

test();



Project 2 and 3 are static libraries. Project 2 has:



#include <string.h>
#include <stdio.h>

void test()

printf("%sn", strchr("x", 'x'));



Project 3 has:



char * strchr(const char * s, int c) // exact signature of MSVC

return "overridden";



I expect output to be overridden but instead it is



x


But if I add this to Project 1:



printf("%sn", strchr("y", 'y'));


The output will be



overridden
overridden


First one from test() in library, second from executable main() directly.



Any suggestions?







windows visual-studio embedded msvcrt






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 at 20:49







jws

















asked Mar 22 at 19:57









jwsjws

471314




471314












  • What have you tried that does not work? Knowing that will save anyone from suggesting things you have already tried, or enable then to see where you might be going wrong if you approach is nearly correct.

    – Clifford
    Mar 22 at 20:29











  • I am not sure how having "only a flash memory for a file system" precludes the use of the standard file system I/O on the test system - that is rather the purpose of the stdio abstraction. A more usual requirement is to emulate an embedded non-volatile persistent storage API using the test environment's file-system. Unless of course it is teh file system itself you are testing.

    – Clifford
    Mar 22 at 20:37











  • Agreed, it uses a special CRT on the embedded device and is the point of using fopen etc. But development tools for the embedded device are sub-par. I like to have the richness of PC tools, such as nice debugging, code coverage, etc., that the embedded device does not offer. So what I need to do is emulate or mock embedded CRT on Windows, but, it's falling into actual Windows CRT instead.

    – jws
    Mar 22 at 20:40











  • If you are doing cross platform compiling and want to test an embedded program on a PC, you will certainly need some conditional preprocessor magic. You are basically writing your own HAL, and including <stdio.h> directly in your files means your are skipping the abstraction layer and going straight for the implementation. So start by creating your own header which will do the necessary wiring, and avoid using standard headers which access hardware directly (like <stdio.h>). It's likely you will want to avoid <stdlib.h> and anything related to malloc too.

    – Groo
    Mar 25 at 8:58












  • @Groo I am able to redirect CRT of a third party lib, which is proprietary and doesn't come with source. It's not necessary to create an entire abstraction layer. (I will give you this - the link solution isn't pretty.)

    – jws
    Mar 25 at 20:17

















  • What have you tried that does not work? Knowing that will save anyone from suggesting things you have already tried, or enable then to see where you might be going wrong if you approach is nearly correct.

    – Clifford
    Mar 22 at 20:29











  • I am not sure how having "only a flash memory for a file system" precludes the use of the standard file system I/O on the test system - that is rather the purpose of the stdio abstraction. A more usual requirement is to emulate an embedded non-volatile persistent storage API using the test environment's file-system. Unless of course it is teh file system itself you are testing.

    – Clifford
    Mar 22 at 20:37











  • Agreed, it uses a special CRT on the embedded device and is the point of using fopen etc. But development tools for the embedded device are sub-par. I like to have the richness of PC tools, such as nice debugging, code coverage, etc., that the embedded device does not offer. So what I need to do is emulate or mock embedded CRT on Windows, but, it's falling into actual Windows CRT instead.

    – jws
    Mar 22 at 20:40











  • If you are doing cross platform compiling and want to test an embedded program on a PC, you will certainly need some conditional preprocessor magic. You are basically writing your own HAL, and including <stdio.h> directly in your files means your are skipping the abstraction layer and going straight for the implementation. So start by creating your own header which will do the necessary wiring, and avoid using standard headers which access hardware directly (like <stdio.h>). It's likely you will want to avoid <stdlib.h> and anything related to malloc too.

    – Groo
    Mar 25 at 8:58












  • @Groo I am able to redirect CRT of a third party lib, which is proprietary and doesn't come with source. It's not necessary to create an entire abstraction layer. (I will give you this - the link solution isn't pretty.)

    – jws
    Mar 25 at 20:17
















What have you tried that does not work? Knowing that will save anyone from suggesting things you have already tried, or enable then to see where you might be going wrong if you approach is nearly correct.

– Clifford
Mar 22 at 20:29





What have you tried that does not work? Knowing that will save anyone from suggesting things you have already tried, or enable then to see where you might be going wrong if you approach is nearly correct.

– Clifford
Mar 22 at 20:29













I am not sure how having "only a flash memory for a file system" precludes the use of the standard file system I/O on the test system - that is rather the purpose of the stdio abstraction. A more usual requirement is to emulate an embedded non-volatile persistent storage API using the test environment's file-system. Unless of course it is teh file system itself you are testing.

– Clifford
Mar 22 at 20:37





I am not sure how having "only a flash memory for a file system" precludes the use of the standard file system I/O on the test system - that is rather the purpose of the stdio abstraction. A more usual requirement is to emulate an embedded non-volatile persistent storage API using the test environment's file-system. Unless of course it is teh file system itself you are testing.

– Clifford
Mar 22 at 20:37













Agreed, it uses a special CRT on the embedded device and is the point of using fopen etc. But development tools for the embedded device are sub-par. I like to have the richness of PC tools, such as nice debugging, code coverage, etc., that the embedded device does not offer. So what I need to do is emulate or mock embedded CRT on Windows, but, it's falling into actual Windows CRT instead.

– jws
Mar 22 at 20:40





Agreed, it uses a special CRT on the embedded device and is the point of using fopen etc. But development tools for the embedded device are sub-par. I like to have the richness of PC tools, such as nice debugging, code coverage, etc., that the embedded device does not offer. So what I need to do is emulate or mock embedded CRT on Windows, but, it's falling into actual Windows CRT instead.

– jws
Mar 22 at 20:40













If you are doing cross platform compiling and want to test an embedded program on a PC, you will certainly need some conditional preprocessor magic. You are basically writing your own HAL, and including <stdio.h> directly in your files means your are skipping the abstraction layer and going straight for the implementation. So start by creating your own header which will do the necessary wiring, and avoid using standard headers which access hardware directly (like <stdio.h>). It's likely you will want to avoid <stdlib.h> and anything related to malloc too.

– Groo
Mar 25 at 8:58






If you are doing cross platform compiling and want to test an embedded program on a PC, you will certainly need some conditional preprocessor magic. You are basically writing your own HAL, and including <stdio.h> directly in your files means your are skipping the abstraction layer and going straight for the implementation. So start by creating your own header which will do the necessary wiring, and avoid using standard headers which access hardware directly (like <stdio.h>). It's likely you will want to avoid <stdlib.h> and anything related to malloc too.

– Groo
Mar 25 at 8:58














@Groo I am able to redirect CRT of a third party lib, which is proprietary and doesn't come with source. It's not necessary to create an entire abstraction layer. (I will give you this - the link solution isn't pretty.)

– jws
Mar 25 at 20:17





@Groo I am able to redirect CRT of a third party lib, which is proprietary and doesn't come with source. It's not necessary to create an entire abstraction layer. (I will give you this - the link solution isn't pretty.)

– jws
Mar 25 at 20:17












2 Answers
2






active

oldest

votes


















1














The linker resolves symbols on a first match basis - priority to separately linked .obj files, then .lib files in the order presented to the linker of the command line. So you can normally override a library symbol just be linking your own replacement. I have never tried it with MSVC, and it is a somewhat brutal approach.



An alternative solution that does not rely on specific linker behaviour is to use the pre-processor to replace standard symbols with your own alternatives. For example:



#if defined _WIN32
#define fopen test_fopen
FILE* test_fopen( const char * filename, const char * mode ) ;
#endif


add the other macros and declarations you need in a header file called testlib.h for example then use a "forced include" (/FI testlib.h in MSVC) to invisibly include the test interface everywhere. Then when built on Windows, all fopen calls will be replaced with test_fopen calling your replacement functions instead.






share|improve this answer























  • MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

    – jws
    Mar 22 at 20:35











  • @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

    – Clifford
    Mar 22 at 20:43












  • Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

    – jws
    Mar 22 at 20:45






  • 1





    Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

    – Clifford
    Mar 22 at 20:58











  • MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

    – jws
    Mar 22 at 21:07



















1














If the problem is caused by CRT usage in a static library only, and linker is finding MSVCRT DLL export lib before it finds private CRT static lib, a workaround might be to force a reference to the private CRT in the executable source files.



Example matching code above, placed in main as a global:



static char * (*p_strchr)(const char *, int) = strchr;


It is less than ideal but it alters linker search order in a reliable way.



More findings



There may be combinations (that I don't totally understand) where the linker will emit a LNK1169 "one or more multiply defined symbols" (more than one definition) linker error. For example, it might say fopen is defined more than once, and if you reverse the order of the private and MS libraries on the command line, then it might say fread is defined more than once. Perhaps it is caused by MSVC lib having DLL export signature, and the private lib being an import symbol. The behavior is hard to determine as it doesn't error on all of the functions that are being overridden.



If it is possible to switch all private libraries to /MT compiler switch (use static CRT), then the LNK1169 problem is resolved.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306993%2freplacing-msvc-crt-function-with-private-implementation%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The linker resolves symbols on a first match basis - priority to separately linked .obj files, then .lib files in the order presented to the linker of the command line. So you can normally override a library symbol just be linking your own replacement. I have never tried it with MSVC, and it is a somewhat brutal approach.



    An alternative solution that does not rely on specific linker behaviour is to use the pre-processor to replace standard symbols with your own alternatives. For example:



    #if defined _WIN32
    #define fopen test_fopen
    FILE* test_fopen( const char * filename, const char * mode ) ;
    #endif


    add the other macros and declarations you need in a header file called testlib.h for example then use a "forced include" (/FI testlib.h in MSVC) to invisibly include the test interface everywhere. Then when built on Windows, all fopen calls will be replaced with test_fopen calling your replacement functions instead.






    share|improve this answer























    • MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

      – jws
      Mar 22 at 20:35











    • @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

      – Clifford
      Mar 22 at 20:43












    • Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

      – jws
      Mar 22 at 20:45






    • 1





      Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

      – Clifford
      Mar 22 at 20:58











    • MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

      – jws
      Mar 22 at 21:07
















    1














    The linker resolves symbols on a first match basis - priority to separately linked .obj files, then .lib files in the order presented to the linker of the command line. So you can normally override a library symbol just be linking your own replacement. I have never tried it with MSVC, and it is a somewhat brutal approach.



    An alternative solution that does not rely on specific linker behaviour is to use the pre-processor to replace standard symbols with your own alternatives. For example:



    #if defined _WIN32
    #define fopen test_fopen
    FILE* test_fopen( const char * filename, const char * mode ) ;
    #endif


    add the other macros and declarations you need in a header file called testlib.h for example then use a "forced include" (/FI testlib.h in MSVC) to invisibly include the test interface everywhere. Then when built on Windows, all fopen calls will be replaced with test_fopen calling your replacement functions instead.






    share|improve this answer























    • MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

      – jws
      Mar 22 at 20:35











    • @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

      – Clifford
      Mar 22 at 20:43












    • Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

      – jws
      Mar 22 at 20:45






    • 1





      Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

      – Clifford
      Mar 22 at 20:58











    • MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

      – jws
      Mar 22 at 21:07














    1












    1








    1







    The linker resolves symbols on a first match basis - priority to separately linked .obj files, then .lib files in the order presented to the linker of the command line. So you can normally override a library symbol just be linking your own replacement. I have never tried it with MSVC, and it is a somewhat brutal approach.



    An alternative solution that does not rely on specific linker behaviour is to use the pre-processor to replace standard symbols with your own alternatives. For example:



    #if defined _WIN32
    #define fopen test_fopen
    FILE* test_fopen( const char * filename, const char * mode ) ;
    #endif


    add the other macros and declarations you need in a header file called testlib.h for example then use a "forced include" (/FI testlib.h in MSVC) to invisibly include the test interface everywhere. Then when built on Windows, all fopen calls will be replaced with test_fopen calling your replacement functions instead.






    share|improve this answer













    The linker resolves symbols on a first match basis - priority to separately linked .obj files, then .lib files in the order presented to the linker of the command line. So you can normally override a library symbol just be linking your own replacement. I have never tried it with MSVC, and it is a somewhat brutal approach.



    An alternative solution that does not rely on specific linker behaviour is to use the pre-processor to replace standard symbols with your own alternatives. For example:



    #if defined _WIN32
    #define fopen test_fopen
    FILE* test_fopen( const char * filename, const char * mode ) ;
    #endif


    add the other macros and declarations you need in a header file called testlib.h for example then use a "forced include" (/FI testlib.h in MSVC) to invisibly include the test interface everywhere. Then when built on Windows, all fopen calls will be replaced with test_fopen calling your replacement functions instead.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Mar 22 at 20:27









    CliffordClifford

    61.3k860128




    61.3k860128












    • MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

      – jws
      Mar 22 at 20:35











    • @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

      – Clifford
      Mar 22 at 20:43












    • Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

      – jws
      Mar 22 at 20:45






    • 1





      Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

      – Clifford
      Mar 22 at 20:58











    • MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

      – jws
      Mar 22 at 21:07


















    • MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

      – jws
      Mar 22 at 20:35











    • @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

      – Clifford
      Mar 22 at 20:43












    • Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

      – jws
      Mar 22 at 20:45






    • 1





      Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

      – Clifford
      Mar 22 at 20:58











    • MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

      – jws
      Mar 22 at 21:07

















    MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

    – jws
    Mar 22 at 20:35





    MSVCRT is not passed as an input. It's pulled from OBJ metadata from compiler /MD or /MT switch.

    – jws
    Mar 22 at 20:35













    @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

    – Clifford
    Mar 22 at 20:43






    @jws : In that case link the replacements as .obj code rather than .lib files. From your edit, it seems that this will work. That said, what is wrong with including the source for the replacements directly in the project since you have shown that to work? It makes for easier debugging and test instrumentation perhaps.

    – Clifford
    Mar 22 at 20:43














    Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

    – jws
    Mar 22 at 20:45





    Right what I really want is to put the CRT lib last on the command line. But there's no way to say "No CRT - I will provide it manually" as far as I can tell. The static or dynamic CRT doesn't make a difference, I tried. The compile time solution won't work for me because some of the code is lib only from third party.

    – jws
    Mar 22 at 20:45




    1




    1





    Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

    – Clifford
    Mar 22 at 20:58





    Could you perhaps switch to using MinGW GCC where you may have more control? That said I don't believe it cannot be done in MSVC - perhaps the linker option /NODEFAULTLIB is what you need.

    – Clifford
    Mar 22 at 20:58













    MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

    – jws
    Mar 22 at 21:07






    MinGW with GCC is a possibility to try. For me is massive toolchain restructure and I'll probably just live with my ugly newly discovered workaround.

    – jws
    Mar 22 at 21:07














    1














    If the problem is caused by CRT usage in a static library only, and linker is finding MSVCRT DLL export lib before it finds private CRT static lib, a workaround might be to force a reference to the private CRT in the executable source files.



    Example matching code above, placed in main as a global:



    static char * (*p_strchr)(const char *, int) = strchr;


    It is less than ideal but it alters linker search order in a reliable way.



    More findings



    There may be combinations (that I don't totally understand) where the linker will emit a LNK1169 "one or more multiply defined symbols" (more than one definition) linker error. For example, it might say fopen is defined more than once, and if you reverse the order of the private and MS libraries on the command line, then it might say fread is defined more than once. Perhaps it is caused by MSVC lib having DLL export signature, and the private lib being an import symbol. The behavior is hard to determine as it doesn't error on all of the functions that are being overridden.



    If it is possible to switch all private libraries to /MT compiler switch (use static CRT), then the LNK1169 problem is resolved.






    share|improve this answer





























      1














      If the problem is caused by CRT usage in a static library only, and linker is finding MSVCRT DLL export lib before it finds private CRT static lib, a workaround might be to force a reference to the private CRT in the executable source files.



      Example matching code above, placed in main as a global:



      static char * (*p_strchr)(const char *, int) = strchr;


      It is less than ideal but it alters linker search order in a reliable way.



      More findings



      There may be combinations (that I don't totally understand) where the linker will emit a LNK1169 "one or more multiply defined symbols" (more than one definition) linker error. For example, it might say fopen is defined more than once, and if you reverse the order of the private and MS libraries on the command line, then it might say fread is defined more than once. Perhaps it is caused by MSVC lib having DLL export signature, and the private lib being an import symbol. The behavior is hard to determine as it doesn't error on all of the functions that are being overridden.



      If it is possible to switch all private libraries to /MT compiler switch (use static CRT), then the LNK1169 problem is resolved.






      share|improve this answer



























        1












        1








        1







        If the problem is caused by CRT usage in a static library only, and linker is finding MSVCRT DLL export lib before it finds private CRT static lib, a workaround might be to force a reference to the private CRT in the executable source files.



        Example matching code above, placed in main as a global:



        static char * (*p_strchr)(const char *, int) = strchr;


        It is less than ideal but it alters linker search order in a reliable way.



        More findings



        There may be combinations (that I don't totally understand) where the linker will emit a LNK1169 "one or more multiply defined symbols" (more than one definition) linker error. For example, it might say fopen is defined more than once, and if you reverse the order of the private and MS libraries on the command line, then it might say fread is defined more than once. Perhaps it is caused by MSVC lib having DLL export signature, and the private lib being an import symbol. The behavior is hard to determine as it doesn't error on all of the functions that are being overridden.



        If it is possible to switch all private libraries to /MT compiler switch (use static CRT), then the LNK1169 problem is resolved.






        share|improve this answer















        If the problem is caused by CRT usage in a static library only, and linker is finding MSVCRT DLL export lib before it finds private CRT static lib, a workaround might be to force a reference to the private CRT in the executable source files.



        Example matching code above, placed in main as a global:



        static char * (*p_strchr)(const char *, int) = strchr;


        It is less than ideal but it alters linker search order in a reliable way.



        More findings



        There may be combinations (that I don't totally understand) where the linker will emit a LNK1169 "one or more multiply defined symbols" (more than one definition) linker error. For example, it might say fopen is defined more than once, and if you reverse the order of the private and MS libraries on the command line, then it might say fread is defined more than once. Perhaps it is caused by MSVC lib having DLL export signature, and the private lib being an import symbol. The behavior is hard to determine as it doesn't error on all of the functions that are being overridden.



        If it is possible to switch all private libraries to /MT compiler switch (use static CRT), then the LNK1169 problem is resolved.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 25 at 20:14

























        answered Mar 22 at 21:06









        jwsjws

        471314




        471314



























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55306993%2freplacing-msvc-crt-function-with-private-implementation%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript