-1

Please help me on this, its really irritating may be because i am doing it for the first time and have no experience.

Aim : to find factorial of a number

. . STEP 1: Creating new project of type VC++ MFC and in templates MFC DLL

STEP 2: Then comes create a regular DLL (MFC shared) i say Yes

STEP 3: I create header file (ServerHeader.h) with the following code

_declspec (dllimport) int factorial(int no);

STEP 4 : I create cpp file (ServerFactorial.cpp) with the code

#include "stdafx.h" 
_declspec (dllexport) int factorial(int no)
{


return no == 0 ? 1 : no * factorial(no-1);
}

STEP 5 : i BUILD the project and everything is fine

STEP 6 : I create another project Client of VC++ Win32 and in templates Win32 Console Application with application setting as EMPTY PROJECT and create a new cpp file (MainClass.cpp) with the code

#include "ServerHeader.h"

#include <iostream.h>

void main()
{


int no,i;


cout<<"Enter number";


cin>>no;


i=factorial(n);  // calling the method


cout<<endl<<i;   // printing ans

}

STEP 7 : I copy the ServerHeader.h, Server.dll, Server.lib (there are 2 library files, one is Object and other is Export) i copy all 4 from Server to Client

STEP 8: Right click on Client project set the linker's general setting and write the path of my lib file which i copied in Client project folder

STEP 9: i build my project i get

Error 1 fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory f:\client\client\mainclass.cpp 2 Client

Sorry for such a long post.

Rameshwar.S.Soni
  • 630
  • 1
  • 8
  • 22

2 Answers2

2

It should be #include <iostream> then you need to include the namespace, std::cout, or include a using namespace std statement in your code.

Jackson
  • 5,627
  • 2
  • 29
  • 48
  • Thanks Jackson, now the cout and cin errors are no more and i am using #include but now i am getting the following errors Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib Client Error 2 fatal error LNK1120: 1 unresolved externals F:\Client\Client\ClientMain.exe 1 Client – Rameshwar.S.Soni Jan 10 '12 at 11:48
  • This usually indicates that you have written a console application but told VS to build a Windows application. If you google for the error text up to LNK1120 you'll find lots of explainations and solutions. – Jackson Jan 10 '12 at 12:21
  • Thanks Jackson for your help, the major problem was the writing console application and telling VS about windows application. Real stupidity!!!!!! Anyways i have 2 books on c++ and they use #include maybe because they must be considering Turbo c++ compiler and this time i was doing c++ on VS so they both are some what different in syntax and we get errors. I have 2 questions(1) Can we integrate the Turbo c++ compiler in VS so that i can use the old syntax...........(2) Or any good books on c++ considering the Microsoft complier and the IDE as VS. Thanks – Rameshwar.S.Soni Jan 10 '12 at 14:10
  • I think your books are probably fairly old, the .h forms have deprecated for some time (see here: http://members.gamedev.net/sicrane/articles/iostream.html). I don't think you can integrate Tubo C++ with VS, I certainly wouldn't want to try and do that! Google for VS2008 and C++ books, there are plenty out there but as I don't own any myself I'm reluctant to recommend one, if pushed I have found Ivor Horton's books to be well written and he has a VS2008 C++ one. – Jackson Jan 10 '12 at 16:50
  • @RameshwarSoni: You can find a book here: [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Throw away Turbo C++ and any book that talks about it: those are over 20 years old by now and completely obsolete. – Cody Gray - on strike Jan 11 '12 at 00:38
1

If you want to use a shared library(dll) you can not call factorial directly. You have to load the dll using LoadLibrary, use the resulted handle and the name of the function in a call to GetProcAddress and call the returned function pointer. To be able to load the function by name, you should declare it in the dll with the extern "C" specificator, so it's name will be exported without decoration. An alternative would be to add a .def file to the dll project and use GetProcAddress with an ordinal. I hope these general directions will help you.