Linking problems with Visual Studio C++ 2008

I create a solution "Mysl" and two projects in that solution:
1-"Utility" project
2-"Main" project
Utility project, in header files have:

//sum.h
class S
{
private:
int a;
int b;
public:
S(int,int);
int sum();
};

and in source files have:

//sum.cpp
#include "sum.h"
S::S(int x1,int x2)
{
a=x1;
b=x2;
}

int S::sum()
{
return a+b;
}

The "Main" project in header files have:

//main.h
#include "C:/Users/bjanku/Desktop/Mysl/Utility/sum.h" // the directory in my PC

and in source files have

//main.cpp
#include <iostream>
#include "sum.h"
using namespace std;

int main()
{
S s(2,3);
cout<<s.sum()<<endl;
return 0;
}

but when i build the solution i find this errors:

Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib Utility


Error 3 error LNK2019: unresolved external symbol "public: int __thiscall S::sum(void)" (?sum@S@@QAEHXZ) referenced in function _main main.obj Main

Error 4 error LNK2019: unresolved external symbol "public: __thiscall S::S(int,int)" (??0S@@QAE@HH@Z) referenced in function _main main.obj Main

Error 5 fatal error LNK1120: 2 unresolved externals C:\Users\bjanku\Desktop\Mysl\D…


Please someone help me?
Last edited on
I believe you are getting this error message because you have used Visual C++ 2008 to create a win32 project but a win32 project does not use main() as the entry point to the program, and so the compiler does not know where to start.

I am using Visual studio 2010 to get this information (by creating a non empty win32 project) but in theory it should be the same... you need to use the following to replace your main()...

1
2
3
4
5
6
7
int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
// your code from main() goes here
}


... or you could create a new console project and copy your current code into that (since console projects use either main() or main(int argc, char *argv[]) as their program entry points).
Last edited on
I replace main() with this
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)

{
S s(1,3);
cout<<s.sum()<<endl;
return 0;
}
but there are other errors:

Error 1 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib Utility

Error 2 fatal error LNK1120: 1 unresolved externals C:\Users\bjanku\Desktop\Mysl\Debug\Utility.exe 1 Utility

Error 3 error C2146: syntax error : missing ';' before identifier '_tWinMain' c:\users\bjanku\desktop\mysl\main\main.cpp 5 Main

Error 4 error C2065: 'HINSTANCE' : undeclared identifier c:\users\bjanku\desktop\mysl\main\main.cpp 5 Main

Error 5 error C2146: syntax error : missing ')' before identifier 'hInstance' c:\users\bjanku\desktop\mysl\main\main.cpp 5 Main

Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\bjanku\desktop\mysl\main\main.cpp 5 Main

Error 7 error C2059: syntax error : ')' c:\users\bjanku\desktop\mysl\main\main.cpp 8 Main

Error 8 error C2143: syntax error : missing ';' before '{' c:\users\bjanku\desktop\mysl\main\main.cpp 10 Main

Error 9 error C2447: '{' : missing function header (old-style formal list?) c:\users\bjanku\desktop\mysl\main\main.cpp 10 Main
I don't think the WinMain suggestion was appropriate here. The errors suggest that you are trying to build two console exe projects : utility.exe (which cannot find main) and main.exe (which find class S)

If you want to build a utility library, you need to create a new project which creates utility.lib rather than utility.exe (I would use a static library here). In this case you then have to link main.exe to utility.lib.

Alternatively, you could just move utility.h and utility.cpp to the main project and compile and link everything as one.
I am new to Visual Studio 2008, please can you explain in more details the second option:

If you want to build a utility library, you need to create a new project which creates utility.lib rather than utility.exe (I would use a static library here). In this case you then have to link main.exe to utility.lib.
As you are new to Visual Studio, I suggest you stick to creating exes till you can deal with all the problems that throws up.

But it you are keen enough, see:

Creating and Using a Static Library (C++)
http://msdn.microsoft.com/en-us/library/ms235627%28v=VS.90%29.aspx
Topic archived. No new replies allowed.