0

(I am using Visual Studio and have never manually linked object files in to executables)

This is my header code:

#ifndef U_H
#define U_H

typedef unsigned int uint;
typedef unsigned short ushort;

class U{
public:
    static uint getX(char* a, uint b, ushort c);
    static uint getY(char* a, uint b, ushort c);
    static uint getZ(char* a, uint b, ushort c);
};

#endif

and source file:

#include "U.h"

uint U::getX(char* a, uint b, ushort c){
    //Stuff
}

uint U::getY(char* a, uint b, ushort c){
    //Stuff
}

uint U::getZ(char* a, uint b, ushort c){
    //Stuff
}

and in main I #include "U.h" and then call the above using U::getY(a,b,c) etc. However I get the following error:

1>main.obj : error LNK2001: unresolved external symbol "unsigned int __cdecl getX(char *,unsigned int,unsigned short)" (?getX@@YAIPEADIG@Z)
1>main.obj : error LNK2001: unresolved external symbol "unsigned int __cdecl getY(char *,unsigned int,unsigned short)" (?getY@@YAIPEADIG@Z)
1>main.obj : error LNK2001: unresolved external symbol "unsigned int __cdecl getZ(char *,unsigned int,unsigned short)" (?getZ@@YAIPEADIG@Z)

I usually forget the U:: part in the source file but this is there??

user997112
  • 29,025
  • 43
  • 182
  • 361
  • 1
    You probably forgot to link `u.obj` into your executable. – Thomas Mar 30 '14 at 17:36
  • @Thomas I am using visual studio and have never had to do that manually before? Could there be another reason? – user997112 Mar 30 '14 at 17:41
  • @user997112, in VS you need to add your U.cpp file to Source Files section in Solution Explorer (http://i.stack.imgur.com/mzPJ3.jpg). – Ivan Mar 30 '14 at 17:42
  • @IvanGrynko its already in there- I keep all my .cpp files in the "Source" section – user997112 Mar 30 '14 at 17:49
  • could you upload a minimal project that has this issue? must be a broken project setting somewhere – paulm Mar 30 '14 at 17:50
  • could you please show code, where you call this functions? Are all cpp files included in Visual studio project? – Sandro Mar 30 '14 at 17:50
  • @Sandro this is a tiny project with a main, 3x header files and 3x source. I'll include some main code in my Q. – user997112 Mar 30 '14 at 17:51
  • Ok solved it- its where I originally had these functions in main class and when I extracted them in to another class I accidently left the function definitions at the top. – user997112 Mar 30 '14 at 17:53
  • So you where calling X:getX() where X didn't have an implementation of X? But then had U:getX() with an implementation somewhere else that wasn't being called/ – paulm Mar 30 '14 at 17:57
  • I was declaring getX() in main (accidentally), but not having a definition because the definition was in U.h – user997112 Mar 30 '14 at 18:08
  • Rebuild your solution. Sometimes it happens. – Joker_vD Mar 30 '14 at 18:16

1 Answers1

0

it seems to be a system setting issue. this kind of thing used to happen to me. anyway, go to [Project] --> [Settings] -->[linker] --> [enable incremental linking], check out the settings here. last time when i ran into the linking problem, i changed it to No and problem got solve.