Problems with basic Hello World program.

I am using the following code....


#include "stdafx.h"
#include <iostream>


int _tmain(int argc, _TCHAR* argv[])
{
std::cout << "Hello, world!\n";
return 0;
}

And I am getting the following error...

Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<<char,struct std::char_traits<char> >(class std::basic_ostream<char,struct std::char_traits<char> > &,char const *)" (__imp_ $ 6DU $char_traits@D@std@@@std@@YAAAV $basic_ostream@DU $char_traits@D@std@@@0@AAV10@PBD@Z) referenced in function _wmain test4.obj


I don't know why but V C++ EE insists on having stdafx.h included otherwise I get about 25 errors. Any idea why this very basic program isn't working The annoyign thing is, I have a couple of files provided for me. A class file, class header and testing file to test the class, in the object is a call to use cout and it runs fine, but if I put it in the main funcation it won't work. Even when I basically copy the contents of the other files into the tester file.

Any ideas

Hope you can help as at the machines at uni, this program runs fine and they have visual studio 2006 and I'd like to be able to bring things home for me to work on.

Thanks in advance...

Ben.


Answer this question

Problems with basic Hello World program.

  • Azeem Sarwar

    I also have to use.. wcout other wise it jsut prints out a memory address I think.

  • divya mittal

    You can embrace all string constants with _T(). That is resolved according to _UNICODE defined or not, so that L is prepended if in unicode builds. So you have _T("Hello World!")

    --
    SvenC


  • johnvarney

    You should use unicode as everything after Windows 95 has been based in Unicode. However, if you are just writing sample apps to learn, and don't want to be bothered by the L's and the w's in front of everything, you can go to project settings, then on the General page there should be a section that says "Use Multibyte" or "Use Unicode", set it to multibyte.

    -Reza


  • KannanPV

    Check to see if your program is using Multibyte or Unicode. This is usually on the front page of the project settings.

    If it is set to Unicode (which it very well might be), then you need to place an L in front of the string.

    std::cout << L"Hello, world!\n";

    This tells it that the string is using wide characters. If your running in unicode, you generally should change the main() function to wmain() also, although that won't keep it from compiling (VC++ usually subs it at build time for you).

    Also, turn off Using Precompiled Headers as they are not necessary for your project and then you can remove the #include "stdafx.h"

    Let me know if that worked. Otherwise we can take a better look.

    -Reza


  • sebastian_v_b

    Did you setup this project with the VC++ project wizard Did you change any options Did you add additional lib or obj files

    --
    SvenC


  • RobDude

    Change your project settings (General node in your project properties) to Multi Byte Character Set, then your code should be "compatible".

    --
    SvenC


  • kawano1h

    Error 3 error LNK2019: unresolved external symbol "public: __thiscall std::ios_base::Init::Init(void)" ( 0Init@ios_base@std@@QAE@XZ) referenced in function "void __cdecl std::`dynamic initializer for '_Ios_init''(void)" ( __E_Ios_init@std@@YAXXZ) test4.obj

    Error 4 error LNK2019: unresolved external symbol "public: __thiscall std::ios_base::Init::~Init(void)" ( 1Init@ios_base@std@@QAE@XZ) referenced in function "void __cdecl std::`dynamic atexit destructor for '_Ios_init''(void)" ( __F_Ios_init@std@@YAXXZ) test4.obj

    Hmmm.... this one looks interesting...
    Warning 1 warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library test4

    I changed the runtime but no luck....

  • Nima_DK

    I've done that and get exactly the same errors...

  • Gabriel3

    Ok, started a new console app. I''m guessing i'd ben messing about with the project setting to much and it caused some wierd errors....

    Ok here is my code... and here is my errors...

    #include "stdafx.h"
    #include <iostream>


    int main()
    {
    std::cout << "Hello, world!\n";
    return 0;
    }

    Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<<char,struct std::char_traits<char> >(class std::basic_ostream<char,struct std::char_traits<char> > &,char const *)" (__imp_ $ 6DU $char_traits@D@std@@@std@@YAAAV $basic_ostream@DU $char_traits@D@std@@@0@AAV10@PBD@Z) referenced in function _main dfgjhkl.obj


    and

    Error 2 fatal error LNK1120: 1 unresolved externals C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\dfgjhkl\Debug\dfgjhkl.exe 1




  • rjodwyer2

    Heh, is this just a V C++ thing as opposed to a normal C++ ide

    Because the code works well on my other compiler on my laptop. I just want to do some basic C++ programming using the VS ide as we work with VC++ at uni and I want to be able transfer my projects to and from there. It works at my uni fine jsut with standard C++, no need for all the

    _T("strin") or L"strings" things.

    Ben.

  • Rob Harris

    Whether I set it to unicode or multibyte, I get the same error if I don't use the "L" before the "hello"

    I'm sorry for being annoying here.

  • Mchafu

    Heh, when I place an L infront of the "hello" is works....

    How do I get it so I don't need to use the "L"

    Oh and thanks :)



  • AndrewBadera

    How do I change the runtime

    Sorry. I am very new at this.

  • AlexCr

    Your cpp is configure to use precompiled headers. You can turn that off from project properties in the C/C++ config section under Precompiled Headers. (Menu Project->Properties, or right click your project in the solution tree view and choose Properties)

    What runtime do you inlude Should be /MT or /MD which should automatically link with the default libs. Check if you have "Ignore All Default Libraries" set. Its in der Linker/Input section of your project configuration properties.

    --
    SvenC


  • Problems with basic Hello World program.