forums.silverfrost.com Forum Index forums.silverfrost.com
Welcome to the Silverfrost forums
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Creating Executable file from DLL and OBJ files
Goto page 1, 2, 3  Next
 
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General
View previous topic :: View next topic  
Author Message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Wed May 27, 2015 8:52 pm    Post subject: Creating Executable file from DLL and OBJ files Reply with quote

Apologize if this is a silly question.

What I am trying to do
I have 2 .for files Ex1.for and Ex2_Real.for.
I have created a Ex2.dll from the Ex2_Real.for file. In the same directory as the Ex2.dll I also have Ex1.for compiled into a Ex1.obj file. I want to compile the Ex1.obj and the Ex2.dll files into a single executable file. In fact, theEx1 .obj has a call to the subroutine that was converted into the Ex2.dll file. I am doing this for testing the possibility of compiling our company's program into a dll file to be used by other 3rd party programs. We only want 1 of our routines to be a dynamic link, accessible at run time to 3rd parties. To mimic this I created 1 dll and 1 obj file. The obj file will not be accessible to the third party, while the dll will be shared with them and can be accessed, like a black box, without us revealing what is inside the dll.

However, I am missing something when trying to convert the .dll and .obj into a single executable file. For testing sake, I have kept the coding within Ex1 and Ex2 simple. Ex1 has 3 variables a, b, and c, where c = a+ b . Ex1 then calls Ex2 with the arguments a, b and c. Ex2 simply displays the output a, b, and c.

I created a Create.lk with the following 2 lines,
load d:\...\Ex1.obj
load d:\...\Ex2.dll
file d:\...\Final.exe

I then created a CreateFinalExe.bat with
slink d:\...\Create.lk

When I run CreateFinalExe.bat, I get a No main, WinMain or LibMain function. I dont have a programming background per se, or a computer science background either. I did some searching on the internet, but could not quite grasp what is exactly going on. Appreciate any help in this regard. Please correct me if any of my understanding is wrong, grossly or subtly.
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1004

PostPosted: Wed May 27, 2015 9:09 pm    Post subject: Reply with quote

Does Ex1.for contain a main program? When you built the DLL, what symbol(s) did you specify as being marked for export?

It would help if, in addition to (or instead of, as appropriate) giving verbal descriptions of what the source files contain, you posted the complete contents of the two files.


Last edited by mecej4 on Wed May 27, 2015 9:12 pm; edited 1 time in total
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Wed May 27, 2015 9:12 pm    Post subject: Reply with quote

Mecej4, Thank you for the quick response.

Here is the code in Ex1
subroutine Ex1
implicit none
real * 8 a, b, c
c
a = 10.0d0
b = 15.0d0
c = a + b
c
write (6,*) "I am from Ex1 - going to call Ex2"
call ex2 (a, b, c)
c
stop
end

and here is the code in Ex2
subroutine Ex2 (a, b, c)
implicit none
real * 8 a, b, c
c
write (6,*) "I am from Ex2 - Called from Ex1"
write (6,*) "a =", a
write (6,*) "b =", b
write (6,*) "c =", c
c
return
end
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1004

PostPosted: Wed May 27, 2015 9:14 pm    Post subject: Reply with quote

You cannot build an EXE unless there is one and only one object that contains a main program.

In file Ex1.for, change 'SUBROUTINE' to 'PROGRAM', and rebuild the EXE.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 5542
Location: Salford, UK

PostPosted: Wed May 27, 2015 9:16 pm    Post subject: Reply with quote

You can not take code from a DLL and link it into an exe.

A DLL is "dynamic" which means that it links at load or run time.

There are basically three ways that work...

1) Just link the original obj files and do not create a library.

2) Create a static library rather than a DLL and use this in the linking process.

3) Create a separate DLL and executable to be used together at run time.
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Wed May 27, 2015 9:26 pm    Post subject: Reply with quote

Mecej4, I changed Subroutine to Program, and rebuild the exe. I continue to get the same error "No main, winmain..."
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Wed May 27, 2015 9:28 pm    Post subject: Reply with quote

PaulLaidler,

So if I understand right, I should only try to compile the Ex1.obj into an executable. Since I already have a dll of Ex2, I do not need to build an exe with it? So at run time, Ex1.obj can access the Ex2.dll without Ex2.dll having been compiled into the executable?
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1004

PostPosted: Wed May 27, 2015 9:37 pm    Post subject: Reply with quote

You have to get all the pieces right, and you have to build in the proper order. You have to get the syntax correct. For example, you cannot use 'c' to start comment lines when the source is in free form.

File Ex2.f90:
Code:
subroutine Ex2 (a, b, c)
implicit none
real a, b, c
!
write (6,*) "I am from Ex2 - Called from Ex1"
write (6,*) "a =", a
write (6,*) "b =", b
write (6,*) "c =", c
!
return
end


Building DLL:
Code:
ftn95 ex2.f90
slink /dll ex2.obj /export:EX2


File Ex1.f90:
Code:
Program Ex1
implicit none
real a, b, c
!
a = 10.0d0
b = 15.0d0
c = a + b
!
write (6,*) "I am from Ex1 - going to call Ex2"
call ex2 (a, b, c)
!
stop
end


Building and running EXE:
Code:
ftn95 ex1.f90
slink ex1.obj ex2.dll
ex1


Output:
Code:
 I am from Ex1 - going to call Ex2
 I am from Ex2 - Called from Ex1
 a =    10.00000
 b =    15.00000
 c =    25.00000
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Wed May 27, 2015 10:03 pm    Post subject: Reply with quote

Mecej4,

Thank you! I am able to get it all together now and see the output I desired.

Thanks very much for your help.

Anand
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Thu May 28, 2015 3:45 pm    Post subject: Calling fortran subroutine from a DLL Reply with quote

Suppose I want to call a Fortran subroutine from a DLL. How would I do it. To revisit, I have 3 files now.
1. Program Ex1.for which will create an executable Ex1.exe
2. Subroutine Ex2.for compiled into a Ex2.dll
3. Subroutine Ex3.for compiled into a Ex3.obj

I get an error 29, call to missing routine, even when I have compiled in Ex3.for as Ex3.obj and linked it with the Ex1.obj.

Once again, apologize if this is dumb question. Thank you for your help.
Back to top
View user's profile Send private message
PaulLaidler
Site Admin


Joined: 21 Feb 2005
Posts: 5542
Location: Salford, UK

PostPosted: Thu May 28, 2015 4:23 pm    Post subject: Reply with quote

Either link Ex3.obj with Ex2.obj into Ex2.dll or create Ex3.dll from Ex3.obj.
The first option is simpler.
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Thu May 28, 2015 4:30 pm    Post subject: Reply with quote

Paul,

So, if I understand correct - all routines that a DLL file calls, should themselves be a DLL or should be compiled into the main DLL (which is independent).
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1004

PostPosted: Thu May 28, 2015 4:36 pm    Post subject: Reply with quote

Anand, from your questions I conclude that you would benefit by reading some documentation on how DLLs and EXEs function and how they are put together.

Having read those, you would realize that once a DLL has been built, you cannot add new subprograms to it. If you changed EX2.FOR after building EX2.DLL, the DLL no longer corresponds to the source code. It is stale, and needs to be discarded and rebuilt.

There is one special case where things are a bit different: if the DLL contains a routine one of whose dummy arguments is EXTERNAL or a pointer to a routine, the code in the DLL will call whichever routine gets passed to the DLL routine as an actual argument -- the actual argument in such cases is sometimes called a callback routine.
Back to top
View user's profile Send private message
anand3162



Joined: 17 Oct 2014
Posts: 26

PostPosted: Thu May 28, 2015 4:50 pm    Post subject: Reply with quote

Mecej4,

If there are any links you could direct me to apart from what is available in FT95 help, I will be thankful.

Yes, I did indeed rebuild my Ex2.dll after I made changes to Ex2.for.
Since Ex3 is being called by Ex2.dll, I attempted to link Ex3.obj into Ex2.dll but cannot seem to get around the problem of error 29.
Back to top
View user's profile Send private message
mecej4



Joined: 31 Oct 2006
Posts: 1004

PostPosted: Thu May 28, 2015 5:05 pm    Post subject: Reply with quote

IF EX2.for contains a call to a routine that is in EX3.for, you could not have built EX2.DLL without linking in EX3.obj when building the DLL -- the link command for building the DLL would have failed, telling you about unsatisfied externals that were responsible. We do not know what is in EX3.for and how you rebuilt the DLL, so it is not possible to be more specific.

You cannot add .OBJ files to a DLL. Unlike a static library, a DLL cannot contain unresolved references to external routines. In many ways, a DLL is more similar to an EXE than a LIB file.

Start with the Wikipedia article on DLLs: http://en.wikipedia.org/wiki/Dynamic-link_library , and see some of the links at the bottom of that page.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forums.silverfrost.com Forum Index -> General All times are GMT + 1 Hour
Goto page 1, 2, 3  Next
Page 1 of 3

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group