C ++中的构建错误,未定义的符号,在这里尝试了其他答案,无法弄清楚

build error in c++, undefined symbols, tried other answers here, cant figure it out

本文关键字:其他 弄清楚 答案 符号 构建 错误 未定义 在这里      更新时间:2023-10-16

我的代码编译良好,但在构建时出现此错误。我知道符号 defs 有些不正常,但我一遍又一遍地检查它们,从我所看到的情况来看,它们都正确定义并具有正确的引用,所以我认为这可能是 IVE 将标头和实现放在一起的方式有问题,但是 idk。请帮忙,请不要仅仅因为问题被问到而关闭它,我没有从其他问题的其他答案中获得任何帮助。

/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/

问题出在您的生成文件中。您没有与实现链接。如果您发布制作文件,我可以进一步提供帮助。

正确的解决方案:如何在 C 语言中链接多个实现文件

其他替代方案:

  • 在主文件中包含.cpp文件的实现(不推荐它,但它会起作用)
  • 将.cpp文件的实现剪切/粘贴到主文件中(等效于上述文件,但不可缩放)