NO

Author Topic: Trouble linking C runtime  (Read 13592 times)

aMarCruz

  • Guest
Re: Trouble linking C runtime
« Reply #15 on: June 19, 2014, 07:49:09 PM »
WRP,
try this:

In menu [Project->Project Options...]
Compiler tab: check 'Omit default library name in object files' (commandline /Zl).
Linker tab: add "msvcrt.lib crt.lib", in this order.

Copy msvcrt.lib to any folder (ex. C:\Dev\Lib) and add this location to Pelles Lib folders.

This is working for me with msvcrt 7.1 (I need this 'cause I'm linkink with winapims.lib to make FLL modules for VFP 9.0, and winapims depends on ms 7.1 runtime).

---edit
Ignore the warning
POLINK: warning: Multiple '.rdata' sections found with different flags (0x40000040 and 0xc0000040).

@beto
« Last Edit: June 19, 2014, 08:02:11 PM by aMarCruz »

WRP

  • Guest
Re: Trouble linking C runtime
« Reply #16 on: June 20, 2014, 04:02:52 AM »
In menu [Project->Project Options...]
Compiler tab: check 'Omit default library name in object files' (commandline /Zl).
Linker tab: add "msvcrt.lib crt.lib", in this order.
Thanks, I gave this a try. Checking /Zl gives

POLINK: error: Unresolved external symbol '__WinMainCRTStartup'.

then adding msvcrt.lib & crt.lib goes back to

POLINK: error: Unresolved external symbol '__imp____argv'.
POLINK: error: Unresolved external symbol '__imp____argc'.

WRP

  • Guest
Re: Trouble linking C runtime
« Reply #17 on: June 20, 2014, 04:08:14 AM »
Here are the results with PellesC v8rc4 32-bit on Windows 7 64-bit. I built your Alarm and Alarm-nomain projects with 5 different versions of MSVCRT placed in C:\Program Files (x860\PellesC\Lib.

Versions of MSVCRT:
1. Made by POLIB from msvcrt.dll
2. Made by POLIB form msvcr100.dll
3. Taken from Windows Driver Kit
4. Taken from Visual C++ 2010
5. Made by tiny_impdef from Tiny C compiler's msvcrt.def

-------------------------------------------------
Building Alarm-nomain
1. msvcrt.lib by POLIB from msvcrt.dll worked
2. msvcrt.lib by POLIB from msvcr100.dll worked
3. msvcrt.lib from WDK worked
4. msvcrt.lib from VS2010 worked

5. msvcrt.lib from TCC gave error:
POLINK: error: Symbol '___argc' is multiply defined: 'msvcrt.lib(msvcrt.dll)' and 'crtmt.lib(_crt0dat.obj)'.
POLINK: error: Symbol '___argv' is multiply defined: 'msvcrt.lib(msvcrt.dll)' and 'crtmt.lib(_crt0dat.obj)'.

If I set -Zl "Omit default library name".

1. msvcrt.lib by POLIB from msvcrt.dll:
2. msvcrt.lib by POLIB from msvcr100.dll:
POLINK: error: Unresolved external symbol '___argv'.
POLINK: error: Unresolved external symbol '___argc'.
POLINK: error: Unresolved external symbol '__WinMainCRTStartup'.

3. msvcrt.lib from WDK:
4. msvcrt.lib from VS2010:
POLINK: error: Unresolved external symbol '___argv'.
POLINK: error: Unresolved external symbol '___argc'.

5. msvcrt.lib from TCC:
POLINK: error: Unresolved external symbol '__WinMainCRTStartup'.

-------------------------------------------------
Building Alarm
1. msvcrt.lib by POLIB from msvcrt.dll:
2. msvcrt.lib by POLIB from msvcr100.dll:
POLINK: error: Unresolved external symbol '__imp____argv'.
POLINK: error: Unresolved external symbol '__imp____argc'.

3. msvcrt.lib from WDK: worked
4. msvcrt.lib from VS2010: worked

5. msvcrt.lib from TCC:
POLINK: error: Symbol '___argc' is multiply defined: 'msvcrt.lib(msvcrt.dll)' and 'crtmt.lib(_crt0dat.obj)'.
POLINK: error: Symbol '___argv' is multiply defined: 'msvcrt.lib(msvcrt.dll)' and 'crtmt.lib(_crt0dat.obj)'.

If I set -Zl "Omit default library name".

All:
POLINK: error: Unresolved external symbol '__WinMainCRTStartup'.

-------------------------------------------------
So, for dynamic binding, I can use the libs from WDK and Visual Studio. I would still like to know, however, why the libs made by POLIB don't work for me, and why TCC's lib doesn't work with PellesC.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Trouble linking C runtime
« Reply #18 on: June 20, 2014, 12:01:52 PM »
Ok I found the problem.
I reproduced the errors (I have update libraries and header from MS so I could always link).
The only problem is the library iupstub.lib that would link with static variables __argc and __argv.
An executable have always to link with a static library containing the entry point (entry point can't be in a DLL which is still not loaded) and uses pocrt.lib for PellesC and libcmt.lib for MS. The two variables __argc and __argv are statically defined in both libraries, so could not be linked together.
Now happen that msvcrt.lib refers to libcmt.lib so automatically include it in the linkage, while the compiler itself includes pocrt.lib as default library. This lead to the multiply defined symbols error.
Using the switch -Zl omits all libraries so the entry point __WinMainCRTStartup is missing.
Adding libcmt.lib to the linker libraries works only using multithreaded DLL.

The best solution is to recompile in pellesC the iupstub.lib and go on.
With a native iupstub.lib you can compile with single threaded and multithreaded static and dynamic libraries with no errors, but compiling in singlethreaded is wrong anyway because the iup library and DLL are created as multithreaded.
Moreover using native iupstub.lib you don't need anymore to specify msvcrt.lib in the linker.  ;)
I have attached a workspace including the fixed alarm project and the iupstub.lib native.  8)
« Last Edit: June 20, 2014, 12:04:22 PM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

WRP

  • Guest
Re: Trouble linking C runtime
« Reply #19 on: June 20, 2014, 09:59:56 PM »
I will give it a try with the iupstub.lib native.

Since dynamic linking worked with the msvcrt.lib from VC++ 2010, I decided to try static linking with the libcmt.lib. Linking with the static version of iup.lib doesn't require iupstub.lib, so I expected to have no problem, but I got these errors. I'm really confused by the conflict of libcmt.lib with itself.

Code: [Select]
POLINK: error: Symbol 'jnedef' is multiply defined: 'libcmt.lib(_cos_.obj)' and 'libcmt.lib(_cos_.obj)'.
POLINK: error: Symbol 'jnedef' is multiply defined: 'libcmt.lib(_sin_.obj)' and 'libcmt.lib(_sin_.obj)'.
POLINK: error: Symbol 'restore_CW' is multiply defined: 'libcmt.lib(_common_.obj)' and 'libcmt.lib(_common_.obj)'.
POLINK: error: Symbol 'CW_is_restored' is multiply defined: 'libcmt.lib(_common_.obj)' and 'libcmt.lib(_common_.obj)'.
POLINK: error: Symbol 'cwdefault' is multiply defined: 'libcmt.lib(_87disp_.obj)' and 'libcmt.lib(_87disp_.obj)'.
POLINK: error: Symbol 'setcw' is multiply defined: 'libcmt.lib(_87disp_.obj)' and 'libcmt.lib(_87disp_.obj)'.
POLINK: error: Symbol '___argc' is multiply defined: 'crt.lib(_crt0dat.obj)' and 'libcmt.lib(crt0dat.obj)'.
POLINK: error: Symbol '___argv' is multiply defined: 'crt.lib(_crt0dat.obj)' and 'libcmt.lib(crt0dat.obj)'.
POLINK: error: Symbol '___wargv' is multiply defined: 'crt.lib(_crt0dat.obj)' and 'libcmt.lib(crt0dat.obj)'.
POLINK: error: Symbol '_exit' is multiply defined: 'crt.lib(exit.obj)' and 'libcmt.lib(crt0dat.obj)'.
POLINK: error: Symbol '_localeconv' is multiply defined: 'crt.lib(localeconv.obj)' and 'libcmt.lib(lconv.obj)'.
POLINK: error: Symbol '_signal' is multiply defined: 'crt.lib(signal.obj)' and 'libcmt.lib(winsig.obj)'.
POLINK: error: Symbol '_raise' is multiply defined: 'crt.lib(raise.obj)' and 'libcmt.lib(winsig.obj)'.
POLINK: error: Symbol '___xi_a' is multiply defined: 'crt.lib(_initex.obj)' and 'libcmt.lib(crt0init.obj)'.
POLINK: error: Symbol '___xi_z' is multiply defined: 'crt.lib(_initex.obj)' and 'libcmt.lib(crt0init.obj)'.
POLINK: error: Symbol '___xt_a' is multiply defined: 'crt.lib(_initex.obj)' and 'libcmt.lib(crt0init.obj)'.
POLINK: error: Symbol '___xt_z' is multiply defined: 'crt.lib(_initex.obj)' and 'libcmt.lib(crt0init.obj)'.
POLINK: error: Symbol '__NLG_Dispatch' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__NLG_Return2' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__NLG_Destination' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__global_unwind2' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__local_unwind2' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__abnormal_termination' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__NLG_Notify1' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.
POLINK: error: Symbol '__NLG_Notify' is multiply defined: 'crt.lib(seh1.obj)' and 'libcmt.lib(exsup.obj)'.


Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Trouble linking C runtime
« Reply #20 on: June 20, 2014, 10:55:14 PM »
Use -Zl switch and eventually include libcmt.lib from MS SDK (or WDDK).
In the compiler tab select multithreaded runtime.
I'm looking for a PellesC workspace to natively compile the iup library. When ready I'll publish it.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

WRP

  • Guest
Re: Trouble linking C runtime
« Reply #21 on: June 21, 2014, 01:54:10 AM »
In the IUP import libs, I replaced the iupstub.lib with your PellesC native version. Now the programs compile without any dependence on MSVCRT. Good work.

I tried static linking again with the -Zl switch and selected multithreaded runtime. (It doesn't seem to matter whether I explicitly include libcmt.lib.) Errors are now reduced to these few. If I set -force:multiple, then it will build and seems to work. I know that with static linking it is best to compile everything with the same compiler, but I'm curious about these errors. It seems that libcmt.lib is conflicting with itself.

Code: [Select]
POLINK: error: Symbol 'jnedef' is multiply defined: 'libcmt.lib(_cos_.obj)' and 'libcmt.lib(_cos_.obj)'.
POLINK: error: Symbol 'jnedef' is multiply defined: 'libcmt.lib(_sin_.obj)' and 'libcmt.lib(_sin_.obj)'.
POLINK: error: Symbol 'restore_CW' is multiply defined: 'libcmt.lib(_common_.obj)' and 'libcmt.lib(_common_.obj)'.
POLINK: error: Symbol 'CW_is_restored' is multiply defined: 'libcmt.lib(_common_.obj)' and 'libcmt.lib(_common_.obj)'.
POLINK: error: Symbol 'cwdefault' is multiply defined: 'libcmt.lib(_87disp_.obj)' and 'libcmt.lib(_87disp_.obj)'.
POLINK: error: Symbol 'setcw' is multiply defined: 'libcmt.lib(_87disp_.obj)' and 'libcmt.lib(_87disp_.obj)'.

The IUP libs are a lot of work to build. A PellesC workspace would be a big convenience.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: Trouble linking C runtime
« Reply #22 on: June 21, 2014, 04:58:06 PM »
In the IUP import libs, I replaced the iupstub.lib with your PellesC native version. Now the programs compile without any dependence on MSVCRT. Good work.
Thanks  8)
The use of static variables, as __argc and __argv, from msvcrt.dll is rare, but it's a problem when linking with a different compiler. But fortunatelyit is used only in iupstub.lib, so recompiling just that library made the job. Anyway I expect that the values of iupstub.lib, linked with pocrt.dll, and that inside the iup.dll, linked to msvcrt.dll, are different (the pointers should be different).
I tried static linking again with the -Zl switch and selected multithreaded runtime. (It doesn't seem to matter whether I explicitly include libcmt.lib.) Errors are now reduced to these few. If I set -force:multiple, then it will build and seems to work. I know that with static linking it is best to compile everything with the same compiler, but I'm curious about these errors. It seems that libcmt.lib is conflicting with itself.

Code: [Select]
POLINK: error: Symbol 'jnedef' is multiply defined: 'libcmt.lib(_cos_.obj)' and 'libcmt.lib(_cos_.obj)'.
POLINK: error: Symbol 'jnedef' is multiply defined: 'libcmt.lib(_sin_.obj)' and 'libcmt.lib(_sin_.obj)'.
POLINK: error: Symbol 'restore_CW' is multiply defined: 'libcmt.lib(_common_.obj)' and 'libcmt.lib(_common_.obj)'.
POLINK: error: Symbol 'CW_is_restored' is multiply defined: 'libcmt.lib(_common_.obj)' and 'libcmt.lib(_common_.obj)'.
POLINK: error: Symbol 'cwdefault' is multiply defined: 'libcmt.lib(_87disp_.obj)' and 'libcmt.lib(_87disp_.obj)'.
POLINK: error: Symbol 'setcw' is multiply defined: 'libcmt.lib(_87disp_.obj)' and 'libcmt.lib(_87disp_.obj)'.

The IUP libs are a lot of work to build. A PellesC workspace would be a big convenience.
This is surely due multiple inclusion of libcmt.lib. Maybe from more libs and runtime DLL's or libs load different versions of the same lib. in this case if the library is the same using -force:multiple should be acceptable.
As already said the best solution is to natively compile libraries. I have already compiled iup.dll and iup.lib. When I'll finish I'll post it.
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide