k-john-gough / gpcp

Gardens Point Component Pascal is an implementation of the Component Pascal Language (CP). There are implementations for both the CLR and the JVM.
Other
59 stars 16 forks source link

Unable to run compiled binaries #26

Closed 1997tn closed 1 year ago

1997tn commented 1 year ago

This is my environment variables:

set CROOT=%userprofile%\Downloads\gpcp-NET set PATH=%PATH%;%CROOT%\bin set CPSYM=.;%CROOT%\symfiles;%CROOT%\symfiles\NetSystem

I compiled Hello.cp example without problems. But when I run it, it failed with:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass
embly 'RTS, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its
 dependencies. The system cannot find the file specified.
   at Hello.Hello..CPmain(String[] A_0)

If I copy Hello.exe into %CROOT%\bin it will run without problems.

k-john-gough commented 1 year ago

Hi 1997tn

Yes, as page 4 of of GettingStartedNET.pdf says

In order for this program to run, it must have access to the public methods of the CP runtime system. The methods are found in the file “RTS.dll”, which must be copied to the working directory. This file is in %CROOT%\bin You may now run the program by the command “Hello”.

Here is a better explanation: When a .NET executable runs it loads other modules/libraries using the following strategy - First is checks for the library in the directory from which the executable (like Hello.exe) was loaded. Failing that it looks in the standard location for system libraries, including, importantly mscorlib.dll, which has most of the .NET runtime.

Of course, it is not just RTS.dll, but any other dependencies as well. My own preferred way of avoiding these problems is to run the compiler so that it places the executable(s) in a subdirectory, traditionally named "bin" of even "bin\debug". Any libraries that you depend on that are NOT part of the .NET runtime need to be copied to the target directory. I usually do this before running gpcp. After that

gpcp /bindir=bin SomeModule.cp to compile a single module, OR CPMake /bindir=bin BaseModule to compile a whole multi-module program in dependency order. You can then explicitly invoke the program by pathname, or make sure the the target directory is on your PATH.

If I remember correctly the reason that the system does not use the user's PATH environment variable, is to avoid security exploits that depend on hacking the PATH variable to insert malware versions of your libraries.

Cheers John


From: 1997tn @.> Sent: Tuesday, 10 January 2023 5:20 AM To: k-john-gough/gpcp @.> Cc: Subscribed @.***> Subject: [k-john-gough/gpcp] Unable to run compiled binaries (Issue #26)

This is my environment variables:

set CROOT=%userprofile%\Downloads\gpcp-NET set PATH=%PATH%;%CROOT%\bin set CPSYM=.;%CROOT%\symfiles;%CROOT%\symfiles\NetSystem

I compiled Hello.cp example without problems. But when I run it, it failed with:

Unhandled Exception: System.IO.FileNotFoundException: Could not load file or ass embly 'RTS, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. The system cannot find the file specified. at Hello.Hello..CPmain(String[] A_0)

If I copy Hello.exe into %CROOT%\bin it will run without problems.

— Reply to this email directly, view it on GitHubhttps://github.com/k-john-gough/gpcp/issues/26, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AEC5EFE4YGRHPLAEUSEPTJLWRRQILANCNFSM6AAAAAATVZKMQA. You are receiving this because you are subscribed to this thread.Message ID: @.***>