open-watcom / open-watcom-v2

Open Watcom V2.0 - Source code repository, Wiki, Latest Binary build, Archived builds including all installers for download.
Other
988 stars 162 forks source link

Cannot create executable when linking library .a file (in my case) #1286

Closed headscott closed 6 months ago

headscott commented 6 months ago

I got the version V2.0 from 29th of april this year.

So I tried to compile this code (test.c) :

#include <stdio.h>
int main(){
  printf("\n");
  return 0;
}

By the following command:

gcc test.c -w -ffreestanding -c -o test.o

gcc here is from djgpp-mingw-gcc1220-standalone from github: https://github.com/andrewwutw/build-djgpp but I think it makes no difference here if its this cross compiler oder standard gnu gcc.

Then I wanted to create an executable from that test.o file by using wlink by the following command:

wlink file test.o file libs/libmsvcrt.a

I also tried these (with exact same result):

wlink file test.o, libs/libmsvcrt.a
wlink file test.o libfile libs/libmsvcrt.a
wlink file test.o library libs/libmsvcrt.a

libmsvcrt.a (path is msys2/mingw32/lib/libmsvcrt.a) is a library I got from my mingw installation, which provides a definition for printf function as far as I know.

Executing these commands does not output any exceptions, but no executable has been created by wlink. Wlink prints this:

wlink file test.o file libs/libmsvcrt.a
Open Watcom Linker Version 2.0 beta Apr 29 2024 01:59:07 (64-bit)
Copyright (c) 2002-2024 The Open Watcom Contributors. All Rights Reserved.
Portions Copyright (c) 1985-2002 Sybase, Inc. All Rights Reserved.
Source code is available under the Sybase Open Watcom Public License.
See https://github.com/open-watcom/open-watcom-v2#readme for details.
loading object files

Thats it, no errors. If I do the same but with this code (another version of test.c):

int main(){
  return 0;
}

and the same steps, but

wlink file test.o

It works fine and test.exe is been generated. So it seems that the .a libraries here is the problem. How can i fix it, because for printf I need a library?

This is just a minimal example, the real project is much bigger, so i think getting all the object files from that .a file via

ar xv libmsvcrt.a

ans searching for all .o files of all the standard c functions I am using and including them while linking by hand doesn't seem right to me, even though this could work.

jmalak commented 6 months ago

I think it is misunderstanding of libraries on various platforms. you are creating static library, you cannot use linker tool but you need to use librarian. If you create all object files which you want to add to some library you can use OW librarian following way wlib -n <library name> <list of object files for adding> see OW documentation

Anyway for linking executable you need linker, the command can be like wlink sys <target system id> name test file { <space separated list of object files> } library { <space separated list of libraries> } see OW documentation

headscott commented 6 months ago

I want to create an executable from my test.o file. It has no reference to any printf function by itself, so I wanted to add the library from my mingw gcc installation and add it while linking, so that there is no missing reference.

If I use ld I would do something like that:

ld test.o -L C:/msys2/mingw32/lib -lmsvcrt

Is there an equivalent way for wlink? wlib would create libraries, but I have my libmsvcrt.a file, so I don't need to create one.

jmalak commented 6 months ago

If target is WIN32 then command for linking should be something like. wlink sys nt name test.exe opt nod file test.o lib libmsvcrt.a but because you not use Watcom CRTL then you need specify your startup code etc.

headscott commented 6 months ago

Okay thank you. I found out, what my problem was: I needed to convert my object files from .o to .obj if I wanted to use that library or I need to make my own library from all needed object files with wlib and add this one with lib option. I am not sure why I just need to convert one of them and not both, but it worked anyways now.

jmalak commented 6 months ago

No you not need to convert it. You have two possibilty