2016-01-19 13 views
0

我的代碼編譯好,但我得到這個錯誤時建立。 我明白有些東西與符號defs不符,但是我一遍又一遍地看到它們,並且從我所看到的,它們都被正確地定義並且具有正確的引用,所以即使我想也許它的方式問題標題和實施一起,但idk。請幫助,請不要關閉它,只是因爲問題已被問到,我沒有得到其他問題的其他答案的任何幫助。在C++中構建錯誤,未定義的符號,嘗試其他答案在這裏,無法弄清楚它

/bin/sh -c '/usr/bin/make -j4 -e -f Makefile' 
----------Building project:[ AreaComp - Debug ]---------- 
/usr/bin/g++ -o ./Debug/AreaComp @"AreaComp.txt" -L. 
Undefined symbols for architecture x86_64: 
"compute::Area::compSq()", referenced from: 
    _main in Areamain.cpp.o 
"compute::Area::compHex()", referenced from: 
    _main in Areamain.cpp.o 
"compute::Area::compOct()", referenced from: 
    _main in Areamain.cpp.o 
"compute::Area::compTri()", referenced from: 
    _main in Areamain.cpp.o 
"compute::Area::compHept()", referenced from: 
    _main in Areamain.cpp.o 
"compute::Area::compPent()", referenced from: 
    _main in Areamain.cpp.o 
ld: symbol(s) not found for architecture x86_64 
clang: error: linker command failed with exit code 1 (use -v to see invocation) 
make[1]: *** [Debug/AreaComp] Error 1 
make: *** [All] Error 2 
====1 errors, 0 warnings==== 

這裏的頭文件:

#ifndef MAIN_SAVITCH_COMPUTE_H 
#define MAIN_SAVITCH_COMPUTE_H 

namespace compute 
{ 
    class Area 
    { 
    public: 

     void compTri(); 
     void compSq(); 
     void compPent(); 
     void compHex(); 
     void compHept(); 
     void compOct(); 

    private: 

     double area; 

    }; 

} 
#endif 

這裏的實現(嘗試作爲.CXX和.cpp ....沒有變化):

#include <iostream> 
#include <cmath> 
#include <cassert> 
#include "Area.h" 



namespace compute 
{ 

double i; 

void Area::compTri() 
{ 
    for(i = 1.00; i < 6.00; i++) 
    { 
     area = (1.00/2.00)i*((pow((-(1/2)i), 2.00)+pow(i,2.00))); 

     cout<< "area for triangle of side length"<< i << "=" << area << endl; 
    } 
} 
void Area::compSq() 
{ 
    for(i = 1.00; i < 6.00; i++) 
    { 
     area = 2.00*i; 

     cout << "area for square of side length" << i << "=" << area << endl; 
    } 
} 
void Area::compPent() 
{ 
    for(i = 1.00; i < 6.00; i++) 
    { 
     area = (1.00/4.00)*sqrt(5.00*(5.00 + 2.00*sqrt(5.00)))*pow(i,2.00); 

     cout << "area for pentagon of side length" << i << "=" << area << endl; 
    } 
} 
void Area::compHex() 
{ 
    for(i = 1.00; i < 6.00; i++) 
    { 
     area = ((3.00*sqrt(3.00))/2.00)*pow(i,2.00); 

     cout << "area for hexagon of side length" << i << "=" << area << endl; 
    } 
} 
void Area::compHept() 
{ 
    for(i = 1.00; i < 6.00; i++) 
    { 
     area = (7.00/4.00)*pow(i,2.00)*(1.00/tan(180.00/7.00)); 

     cout << "area for heptagon of side length" << i << "=" << area << endl; 
    } 
} 
void Area::compOct() 
{ 
    for(i = 1.00; i < 6.00; i++) 
    { 
     area = 2.00*(1.00+sqrt(2.00))*pow(i,2.00); 

     cout << "area for octagon of side length" << i << "=" << area << endl; 
    } 
    } 
} 

這裏的主文件:

#include <iostream> 
#include <cassert> 
#include <cmath> 
#include "Area.h" 

using namespace std; 
using namespace compute; 

int main(int argc, char **argv) 
{ 

int a; 

cout <<"this program will compute the area of a polygon with between 3 & 8"<< endl; 


cout << "how many sides does your polygon have?" << endl; 


cin >> a; 


assert(a >= 3 && a <= 8); 


Area find; 



if(a == 3) 
{ 
    find.compTri(); 
} 
else if(a == 4) 
{ 
    find.compSq(); 
} 
else if(a == 5) 
{ 
    find.compPent(); 
} 
else if(a == 6) 
{ 
    find.compHex(); 
} 
else if(a == 7) 
{ 
    find.compHept(); 
} 
else 
{ 
    find.compOct(); 
} 

return 0; 
}; 

創建文件:

## 
## Auto Generated makefile by CodeLite IDE 
## any manual changes will be erased  
## 
## Debug 
ProjectName   :=AreaComp 
ConfigurationName  :=Debug 
WorkspacePath   := "/Users/tuckerfowler/Documents/CS3304" 
ProjectPath   := "/Users/tuckerfowler/Documents/CS3304 /AreaComp" 
IntermediateDirectory :=./Debug 
OutDir     := $(IntermediateDirectory) 
CurrentFileName  := 
CurrentFilePath  := 
CurrentFileFullPath := 
User     :=Tucker Fowler 
Date     :=18/01/2016 
CodeLitePath   :="/Users/tuckerfowler/Library/Application  Support/codelite" 
LinkerName    :=/usr/bin/g++ 
SharedObjectLinkerName :=/usr/bin/g++ -dynamiclib -fPIC 
ObjectSuffix   :=.o 
DependSuffix   :=.o.d 
PreprocessSuffix  :=.i 
DebugSwitch   :=-g 
IncludeSwitch   :=-I 
LibrarySwitch   :=-l 
OutputSwitch   :=-o 
LibraryPathSwitch  :=-L 
PreprocessorSwitch  :=-D 
SourceSwitch   :=-c 
OutputFile    :=$(IntermediateDirectory)/$(ProjectName) 
Preprocessors   := 
ObjectSwitch   :=-o 
ArchiveOutputSwitch := 
PreprocessOnlySwitch :=-E 
ObjectsFileList  :="AreaComp.txt" 
PCHCompileFlags  := 
MakeDirCommand   :=mkdir -p 
LinkOptions   := 
IncludePath   := $(IncludeSwitch). $(IncludeSwitch). 
IncludePCH    := 
RcIncludePath   := 
Libs     := 
ArLibs     := 
LibPath    := $(LibraryPathSwitch). 

## 
## Common variables 
## AR, CXX, CC, AS, CXXFLAGS and CFLAGS can be overriden using an environment variables 
## 
AR  := /usr/bin/ar rcu 
CXX  := /usr/bin/g++ 
CC  := /usr/bin/gcc 
CXXFLAGS := -g -O0 -Wall $(Preprocessors) 
CFLAGS := -g -O0 -Wall $(Preprocessors) 
ASFLAGS := 
AS  := /usr/bin/as 


## 
## User defined environment variables 
## 
CodeLiteDir:=/Users/tuckerfowler/Downloads/codelite.app/Contents/SharedSupport/ 
Objects0=$(IntermediateDirectory)/main.cpp$(ObjectSuffix) 



Objects=$(Objects0) 

## 
## Main Build Targets 
## 
.PHONY: all clean PreBuild PrePreBuild PostBuild MakeIntermediateDirs 
all: $(OutputFile) 

$(OutputFile): $(IntermediateDirectory)/.d 
$(Objects) 
@$(MakeDirCommand) $(@D)@echo "" > $(IntermediateDirectory)/.d 
@echo $(Objects0) > $(ObjectsFileList) 
$(LinkerName) 
$(OutputSwitch) 
$(OutputFile) 
@$(ObjectsFileList) 
$(LibPath) 
$(Libs) 
$(LinkOptions) 

MakeIntermediateDirs: 
@test -d ./Debug || $(MakeDirCommand) ./Debug 


$(IntermediateDirectory)/.d: 
@test -d ./Debug || $(MakeDirCommand) ./Debug 

PreBuild: 


## 
## Objects 
## 
$(IntermediateDirectory)/main.cpp 
$(ObjectSuffix):main.cpp 
$(IntermediateDirectory)/main.cpp 
$(DependSuffix) 
$(CXX) 
$(IncludePCH) 
$(SourceSwitch) "/Users/tuckerfowler/Documents/CS3304/AreaComp/main.cpp" 
$(CXXFLAGS) 
$(ObjectSwitch) 
$(IntermediateDirectory)/main.cpp 
$(ObjectSuffix) 
$(IncludePath) 
$(IntermediateDirectory)/main.cpp 
$(DependSuffix): main.cpp 
@$(CXX) 
$(CXXFLAGS) 
$(IncludePCH) 
$(IncludePath)-MG-MP-MT 
$(IntermediateDirectory)/main.cpp 
$(ObjectSuffix) -MF 
$(IntermediateDirectory)/main.cpp 
$(DependSuffix) -MM "main.cpp" 

    $(IntermediateDirectory)/main.cpp 
    $(PreprocessSuffix): main.cpp 
    @$(CXX) 
    $(CXXFLAGS) 
    $(IncludePCH) 
    $(IncludePath) 
    $(PreprocessOnlySwitch) 
    $(OutputSwitch) 
    $(IntermediateDirectory)/main.cpp 
    $(PreprocessSuffix) "main.cpp" 


-include $(IntermediateDirectory)/*$(DependSuffix) 
## 
## Clean 
## 
clean: 
    $(RM) -r ./Debug/ 
+0

的可能的複製[什麼是不確定的參照的文ce /無法解析的外部符號錯誤,我該如何解決它?](http://stackoverflow.com/questions/12573816/what-is-an-undefined-reference-unresolved-external-symbol-error-and-how-do -i-fix) – IInspectable

回答

0

問題出在你的makefile。你沒有鏈接到實現。如果你發佈了Makefile,我可以進一步提供幫助。

正確的解決方案: How to link multiple implementation files in C

其他替代方案:

  • 包括在主文件.cpp文件中實現(不建議使用它,但它會工作)
  • 剪切/粘貼實現.cpp文件到主文件(等效於上面,但不能擴展)
+0

現在添加mk文件 – TuckerB

+0

我從來沒有使用過CodeLite,但我知道你需要將你的實現文件添加到你的項目中。我發現了一個SO:http://stackoverflow.com/questions/32327212/how-do-you-add-a-file-to-an-existing-project-in-codelite – TinyTheBrontosaurus

+0

完成,其上,它只是很多閱讀 – TuckerB

相關問題