1
votes

i have n number of cpp files in a project.'A' is the source file which will have main function,'B' is an another source file which contains function definition which will be used by 'A'.

A.cpp #include "stdafx.h" #include "b.h" int main() { add(5,4); return 0; }

B.h

#include "stdafx.h" void add(int a ,int b);

B.cpp

#include "stdafx.h" void add(int a,int b) { cout<<(a+b); }

but the build order is like a.cpp after b.cpp so the add function should be unresolved by linker. how can i solve the build order problem?

Edit 1:my build file log:-

Build started 22-11-2014 15:57:11. 1>Project "C:\Users\Admin\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.vcxproj" on node 2 (Build target(s)). 1>Link: C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\bin\link.exe /ERRORREPORT:PROMPT /OUT:"C:\Users\Admin\Documents\Visual Studio 2013\Projects\ConsoleApplication2\Release\ConsoleApplication2.exe" /INCREMENTAL:NO /NOLOGO /LIBPATH:D:\Glut /LIBPATH:D:\OpenCV\opencv\build\x86\vc11\lib opencv_core246.lib opencv_features2d246.lib opencv_haartraining_engine.lib opencv_calib3d246.lib opencv_highgui246.lib opencv_imgproc246.lib opencv_legacy246.lib opencv_ml246.lib opencv_objdetect246.lib opencv_video246.lib glut32.lib asmlibrary.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /MANIFEST /MANIFESTUAC:"level='asInvoker' uiAccess='false'" /manifest:embed /DEBUG /PDB:"C:\Users\Admin\Documents\Visual Studio 2013\Projects\ConsoleApplication2\Release\ConsoleApplication2.pdb" /SUBSYSTEM:CONSOLE /OPT:REF /OPT:ICF /LTCG /TLBID:1 /DYNAMICBASE /NXCOMPAT /IMPLIB:"C:\Users\Admin\Documents\Visual Studio 2013\Projects\ConsoleApplication2\Release\ConsoleApplication2.lib" /MACHINE:X86 /SAFESEH Release\CLM.obj Release\FCheck.obj

     Release\FDet.obj

     Release\IO.obj

     Release\Patch.obj

     Release\PAW.obj

     Release\PDM.obj

     Release\Tracker.obj

     Release\updated_facetracker_v2.obj

     Release\glm.obj

     Release\glmimg.obj

     Release\glmimg_devil.obj

     Release\glmimg_jpg.obj

     Release\glmimg_png.obj

     Release\glmimg_sdl.obj

     Release\glmimg_sim.obj

     Release\glm_util.obj

     Release\stdafx.obj

 1>updated_facetracker_v2.obj : error LNK2001: unresolved external symbol "void __cdecl glmDraw(struct _GLMmodel *,unsigned int)" (?glmDraw@@YAXPAU_GLMmodel@@I@Z)
 1>updated_facetracker_v2.obj : error LNK2001: unresolved external symbol "struct _GLMmodel * __cdecl glmReadOBJ(char *)" (?glmReadOBJ@@YAPAU_GLMmodel@@PAD@Z)
 1>C:\Users\Admin\Documents\Visual Studio 2013\Projects\ConsoleApplication2\Release\ConsoleApplication2.exe : fatal error LNK1120: 2 unresolved externals
 1>Done Building Project "C:\Users\Admin\Documents\Visual Studio 2013\Projects\ConsoleApplication2\ConsoleApplication2\ConsoleApplication2.vcxproj" (Build target(s)) -- FAILED.

Build FAILED.

Time Elapsed 00:00:00.81

this is the original projects build file here glm.c is compiled built after the updated_facetracker_v2.cpp file and glm.c did not have any error."

1
You should have a look at the error window to easily see all of your errors and identify the first one which occurs.sjdowling
That error is not related to the example you gave, however, there should be a compilation errorr higher up.tillaert
above example is for easy understandingDinesh Kannan
@sjdowling my error window contanins only the unresolved external symbols error from the main method cpp fileDinesh Kannan

1 Answers

2
votes

Your linker fais, because B.cpp doesn't compile. The compiler fails to create an object for B.cpp. Since the object isn't there, the linker can't find it, and gives the error. There should be an error about B.cpp further up the error log.

Change B.cpp from:

#include "stdafx.h"
void add(int a,int b)
{
    cout<<(a+b);
}

to:

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

void add(int a,int b)
{
    std::cout<<(a+b) << std::endl;
}

Build order doesn't matter: The compiler takes each source file and creates an object file. The linker takes the object files and tries to link those to your target executable. The order your compiler generates the object files shouldn't matter.

Also, your main is faulty too:

change:

      add(5+4);

to:

      add(5, 4);

You are trying to link against add(int) while your function in B.cpp has the signature add(int, int) which is a different function according to the linker.