我正在嘗試在運行 Linux 的外部計算機上運行 c 程式,但我在編譯時遇到問題。該程式在我個人 Mac 上的 Visual Studio Code 中編譯得很好...
我的編程和計算機知識有些有限,但我相信計算機使用的是 GNU 編譯器,并且經過一些谷歌搜索。至少它在我的 Mac 上使用了與 VS Code 不同的東西,因為我必須更改 Makefile。
當我使用命令“make”來構建我的程式(由幾個 .h 和 .cpp 檔案組成)時,我遇到了一些我不知道如何解決的錯誤,但我認為它與我的兩個有關“子問題”和“標簽”類(有關錯誤代碼,請參閱本文底部)
兩個類都有成員函式,它們將另一個作為引數。因此,我在定義標簽類之前使用了子問題類的前向宣告。這是我在 Label.h 中代碼的簡化版本,包括錯誤訊息中參考的函式:
#pragma once
#include "Duals.h"
#include "Nodes.h"
#include <memory>
#include <iostream>
class Subproblem; // forward declaration
class Label : public std::enable_shared_from_this<Label> {
// private members
public:
Label();
std::vector<std::shared_ptr<Node> > const PossibleMoves(Subproblem* const sp, int startLB, int startUB, int endLB, int endUB);
std::shared_ptr<Label> ExtendLabel(std::shared_ptr<Node> destinationNode, Subproblem* const sp, Duals duals);
};
bool CheckDominance(std::shared_ptr<Label> l1, std::shared_ptr<Label> l2);
std::ostream& operator<<(std::ostream& os, const std::shared_ptr<Label> l);
bool operator< (const std::shared_ptr<Label>& lhs, const std::shared_ptr<Label>& rhs);
有誰知道可能是什么問題以及我如何解決它?我正在使用 c 17 和 g 。我是這個論壇的新手,如果我的描述中有不清楚或遺漏的地方,我很抱歉。
g -std=c 17 -c -o Subproblem.o Subproblem.cpp
g -o program.exe -I/share/apps/gurobi/9.5.1/include/ main.o BBnode.o BBtree.o Heuristic.o Label.o MasterProblem.o Nodes.o Pattern.o Solution.o Subproblem.o TestInstance.o Utilities.o -lm -L/share/apps/gurobi/9.5.1/lib -lgurobi_g 5.2 -lgurobi_c -lgurobi95
/usr/bin/ld.gold: error: Label.o:1:1: invalid character
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'Label::PossibleMoves(Subproblem*, int, int, int, int)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'operator<<(std::ostream&, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'Label::ExtendLabel(std::shared_ptr<Node>, Subproblem*, Duals)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'operator<<(std::ostream&, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'CheckDominance(std::shared_ptr<Label>, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'operator<<(std::ostream&, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'CheckDominance(std::shared_ptr<Label>, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'operator<<(std::ostream&, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'CheckDominance(std::shared_ptr<Label>, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function Subproblem::FindPaths(Duals, int, int, int, int): error: undefined reference to 'CheckDominance(std::shared_ptr<Label>, std::shared_ptr<Label>)'
Subproblem.o:Subproblem.cpp:function bool __gnu_cxx::__ops::_Iter_less_iter::operator()<__gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > >, __gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > > >(__gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > >, __gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > >) const: error: undefined reference to 'operator<(std::shared_ptr<Label> const&, std::shared_ptr<Label> const&)'
Subproblem.o:Subproblem.cpp:function bool __gnu_cxx::__ops::_Val_less_iter::operator()<std::shared_ptr<Label>, __gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > > >(std::shared_ptr<Label>&, __gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > >) const: error: undefined reference to 'operator<(std::shared_ptr<Label> const&, std::shared_ptr<Label> const&)'
Subproblem.o:Subproblem.cpp:function bool __gnu_cxx::__ops::_Iter_less_val::operator()<__gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > >, std::shared_ptr<Label> >(__gnu_cxx::__normal_iterator<std::shared_ptr<Label>*, std::vector<std::shared_ptr<Label>, std::allocator<std::shared_ptr<Label> > > >, std::shared_ptr<Label>&) const: error: undefined reference to 'operator<(std::shared_ptr<Label> const&, std::shared_ptr<Label> const&)'
collect2: error: ld returned 1 exit status
make: *** [program.exe] Error 1
這是我的 Makefile:
CXX=g
CXXFLAGS= -std=c 17
src = $(wildcard *.cpp)
obj = $(src:.cpp=.o)
dep = $(obj:.o=.d)
LDFLAGS = -lm -L$(GUROBI_HOME)/lib -lgurobi_g 5.2 -lgurobi_c -lgurobi95
CPLUS_INCLUDE_PATH = -I$(GUROBI_HOME)/include/
all: program.exe
program.exe : main.o BBnode.o BBtree.o Heuristic.o Label.o MasterProblem.o Nodes.o Pattern.o Solution.o Subproblem.o TestInstance.o Utilities.o
$(CXX) -o program.exe $(CPLUS_INCLUDE_PATH) main.o BBnode.o BBtree.o Heuristic.o Label.o MasterProblem.o Nodes.o Pattern.o Solution.o Subproblem.o TestInstance.o Utilities.o $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) main.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) BBnode.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) BBtree.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Heuristic.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Label.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) MasterProblem.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Nodes.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Pattern.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Solution.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Subproblem.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) TestInstance.cpp $(LDFLAGS)
#$(CXX) -c $(CPLUS_INCLUDE_PATH) Utilities.cpp $(LDFLAGS)
-include $(dep)
.PHONY: clean
clean:
rm -f VRPTW.exe $(obj)
.PHONY: cleandep
cleandep:
rm -f $(dep)
Label.cpp 的內容:
#include #include "Label.h"
#include "Subproblem.h"
#include <iostream>
#include <cstdlib>
#include <iterator>
#include <vector>
std::vector<std::shared_ptr<Node> > const
Label::PossibleMoves(Subproblem* const sp, int startLB, int startUB, int endLB, int endUB) { /* implementation */ }
std::shared_ptr<Label> Label::ExtendLabel(std::shared_ptr<Node> destinationNode, Subproblem* const sp, Duals duals){ /* implementation */ }
bool CheckDominance(std::shared_ptr<Label> l1, std::shared_ptr<Label> l2){ /* implementation */}
std::ostream& operator<<(std::ostream& os, const std::shared_ptr<Label> l){ /* implementation */}
bool operator< (const std::shared_ptr<Label>& lhs, const std::shared_ptr<Label>& rhs){ /* implementation*/ }
uj5u.com熱心網友回復:
如果您仔細查看錯誤訊息,您會發現引數有所不同。例如,抱怨的錯誤Label::PossibleMoves(Subproblem*, int, int, int, int)需要 a Subproblem*,而不是Subproblem *const您的頭檔案和實作檔案中所寫的 a 。
這讓我覺得您正在鏈接舊版本的,在您對和Subproblem.o進行一些更改后尚未重新編譯該版本。Label.cppLabel.h
這是有道理的,因為Makefile依賴于關于如何從 a 生成 a的隱含規則,并且隱含規則假定僅依賴于。它不會掃描包含的標題以查看那里是否有任何更改;該任務通常由諸如 autotools 或 CMake 之類的 makefile 生成器完成,或者手動寫入自身。.o.cpp.o .cppMakefile
Makefile在(已注釋掉)規則的舊版本中,每次發生更改時都會重新編譯program.exe一堆$(CXX)呼叫。.cpp一個簡單但粗略的解決方案。
如果您只需要編譯一次并繼續您的生活,請嘗試洗掉所有.o檔案并從頭開始構建。我懷疑你會擺脫這些錯誤(也許會得到令人興奮的新錯誤作為回報)。
如果您需要適當的解決方案,請查看 CMake 或其他可以檢測源檔案之間依賴關系的 makefile 生成器或構建工具。
uj5u.com熱心網友回復:
這還不是答案,但嘗試使用這個 Makefile:
CXX=g
CXXFLAGS= -std=c 17 -O2 -g
INCLUDES = -I$(GUROBI_HOME)/include
srcs = $(wildcard *.cpp)
objs = $(srcs:.cpp=.o)
deps = $(objs:.o=.d)
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCLUDES) -c $< -o $@
GUROBI_HOME=gurobi52
LIBS = -lm -L$(GUROBI_HOME)/lib -lgurobi_g 5.2 -lgurobi_c -lgurobi95
LDFLAGS =
all: program
program : $(objs)
$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)
-include $(dep)
.PHONY: clean
clean:
rm -f $(objs)
.PHONY: cleandep
cleandep:
rm -f $(deps)
希望它更清晰,并從那里開始。
鏈接中的自動變數$(CXX) $(LDFLAGS) -o $@ $^ $(LIBS)等價于$(CXX) $(LDFLAGS) -o program $(objs) $(LIBS).
閱讀@Thomas 的答案,您需要使用$ make clean.
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/477233.html
上一篇:呼叫洗掉的自定義unique_ptr洗掉器有什么用?
下一篇:如何在C 中回傳模板串列?
