我需要编译一个C ++项目,该项目将使用项目中的外部库和头文件进行线性编程COIN-OR。它包含三个.cpp文件,我已成功编译成.o文件。我的主要问题是接下来要做什么。由于所有文件都是在没有错误的情况下编译的,我认为它们至少应该给我一些结果。
如何为linux创建可执行文件?
我试过了
gcc myfile.o -o main
它不起作用,它会输出一个非常大的错误文本块。
如果需要,这是Makefile:
#
P=farmer
#
EXE=$(P)
OBJS=main-farmer.o model-farmer.o param-farmer.o pm.h
ADDLIBS=-D.
ADDINCFLAGS=-I.
SRCDIR=~/coin-projects
##########
CXX=g++
CXXFLAGS=-O3 -fomit-frame-pointer -pipe -DNDEBUG -pedantic-errors -Wimplicit -Wparentheses -Wreturn-type -Wcast-qual -Wall -Wpointer-arith -Wwrite-strings -Wconversion
CXXLINKFLAGS=-Wl,--rpath -Wl,/installed/CoinAll/lib
CC=gcc
CFLAGS=-03 -fomit-frame-pointer -pipe -DNDEBUG -pedantic-errors -Wimplicit -Wparentheses -Wsequence-point -Wreturn-type -Wcast-qual -Wall
COININCDIR=/installed/CoinAll/include/coin
# COIN-OR libs
COINLIBDIR=/installed/CoinAll/lib
# Clp
LIBS=-L$(COINLIBDIR) -lCbc -lCgl -lOsiClp -lOsi -lClp -lCoinUtils -lm \
`cat $(COINLIBDIR)/cgl_addlibs.txt` \
`cat $(COINLIBDIR)/clp_addlibs.txt` \
`cat $(COINLIBDIR)/coinutils_addlibs.txt`
CLEANFILES=\
addBits.o addBits \
addColumns.o addColumns \
addRows.o addRows \
decompose.o decompose \
defaults.o defaults \
driver2.o driver2 \
driver.o driver \
driverC.o driverC \
dualCuts.o dualCuts \
ekk.o ekk \
ekk_interface.o ekk_interface \
hello.o hello \
makeDual.o makeDual \
minimum.o minimum \
network.o network \
piece.o piece \
rowColumn.o rowColumn \
sprint2.o sprint2 \
sprint.o sprint \
testBarrier.o testBarrier \
testBasis.o testBasis \
testGub2.o testGub2 \
testGub.o testGub \
testQP.o testQP \
useVolume.o useVolume
# Part 3
#
all: $(EXE)
.SUFFIXES: .cpp .c .o .obj
$(EXE): $(OBJS)
bla=;
for file in $(OBJS); do bla="$$bla `$(CYGPATH_W) $$file`"; done; \
$(CXX) $(CXXLINKFLAGS) $(CXXFLAGS) -o $@ $$bla $(ADDLIBS) $(LIBS)
####
########
############
########
####
clean:
rm -rf $(CLEANFILES)
.cpp.o:
$(CXX) $(CXXFLAGS) $(INCL) -c -o $@ `test -f '$<' || echo '$(SRCDIR)/'`$<
.cpp.obj:
$(CXX) $(CXXFLAGS) $(INCL) -c -o $@ `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(SRCDIR)/$<'; fi`
.c.o:
$(CC) $(CFLAGS) $(INCL) -c -o $@ `test -f '$<' || echo '$(SRCDIR)/'`$<
.c.obj:
$(CC) $(CFLAGS) $(INCL) -c -o $@ `if test -f '$<'; then $(CYGPATH_W) '$<'; else $(CYGPATH_W) '$(SRCDIR)/$<'; fi`
这是我在尝试命令时收到的错误 g ++ main-farmer.o model-farmer.o param-farmer.o -o main
错误讯息:
model-farmer.o: In function models(double*, double (*) [3], double*, double*, double*, double (*) [3], double (*) [3], double (*)
[3], double (*) [3][3], double (*) [6][3])':
model-farmer.cpp:(.text+0x0): multiple definition ofmodels(double*, double () [3], double, double*, double*, double () [3], double () [3], double () [3], double () [3][3], double () [6][3])' main-farmer.o:main-farmer.cpp:(.text+0x60): first defined here param-farmer.o: In function param(double*, double (*) [3], double*,
double*, double*, double (*) [3], double (*) [3], double (*) [3],
double (*) [3][3], double (*) [6][3], double*, double*, double*,
double*, double*, int*, int*, double*, int&)':
param-farmer.cpp:(.text+0x0): multiple definition ofparam(double, double () [3], double, double*, double*, double () [3], double () [3], double () [3], double () [3][3], double () [6][3], double, double*, double*, double*, double*, int*, int*, double*, int&)' main-farmer.o:main-farmer.cpp:(.text+0x590): first defined here main-farmer.o: In function main':
main-farmer.cpp:(.text.startup+0x16e): undefined reference to
OsiClpSolverInterface::OsiClpSolverInterface()' main-farmer.cpp:(.text.startup+0x198): undefined reference to CoinPackedMatrix::CoinPackedMatrix(bool, int const*, int const*,
double const*, int)' main-farmer.cpp:(.text.startup+0x1cb): undefined
reference toOsiClpSolverInterface::loadProblem(CoinPackedMatrix const&, double const*, double const*, double const*, double const*, double const*)' main-farmer.cpp:(.text.startup+0x2fc): undefined reference to ClpModel::setOptimizationDirection(double)'
main-farmer.cpp:(.text.startup+0x32c): undefined reference to
OsiClpSolverInterface::setInteger(int)' main-farmer.cpp:(.text.startup+0x340): undefined reference to OsiClpSolverInterface::setInteger(int)'
main-farmer.cpp:(.text.startup+0x354): undefined reference to
OsiClpSolverInterface::setInteger(int)' main-farmer.cpp:(.text.startup+0x361): undefined reference to OsiClpSolverInterface::initialSolve()'
main-farmer.cpp:(.text.startup+0x36e): undefined reference to
OsiClpSolverInterface::isProvenPrimalInfeasible() const' main-farmer.cpp:(.text.startup+0x3ab): undefined reference to CoinPackedMatrix::~CoinPackedMatrix()'
main-farmer.cpp:(.text.startup+0x3b8): undefined reference to
OsiClpSolverInterface::~OsiClpSolverInterface()' main-farmer.cpp:(.text.startup+0x3de): undefined reference to OsiClpSolverInterface::isProvenOptimal() const'
main-farmer.cpp:(.text.startup+0x3f3): undefined reference to
OsiClpSolverInterface::getObjValue() const' main-farmer.cpp:(.text.startup+0x44d): undefined reference to OsiClpSolverInterface::getColSolution() const'
main-farmer.cpp:(.text.startup+0x478): undefined reference to
OsiClpSolverInterface::getColSolution() const' main-farmer.cpp:(.text.startup+0x4a4): undefined reference to OsiClpSolverInterface::getColSolution() const'
main-farmer.cpp:(.text.startup+0x520): undefined reference to
OsiClpSolverInterface::getColSolution() const' main-farmer.cpp:(.text.startup+0x551): undefined reference to OsiClpSolverInterface::getColSolution() const'
main-farmer.o:main-farmer.cpp:(.text.startup+0x582): more undefined
references toOsiClpSolverInterface::getColSolution() const' follow main-farmer.o: In function main':
main-farmer.cpp:(.text.startup+0x92f): undefined reference to
OsiClpSolverInterface::branchAndBound()' main-farmer.cpp:(.text.startup+0x94a): undefined reference to OsiClpSolverInterface::writeMps(char const*, char const*, double)
const' main-farmer.cpp:(.text.startup+0x957): undefined reference to
OsiClpSolverInterface::getObjValue() const' main-farmer.cpp:(.text.startup+0x96e): undefined reference to OsiClpSolverInterface::isProvenOptimal() const'
main-farmer.cpp:(.text.startup+0x9d6): undefined reference to
OsiClpSolverInterface::getObjValue() const' main-farmer.cpp:(.text.startup+0xa32): undefined reference to OsiClpSolverInterface::getColSolution() const'
main-farmer.cpp:(.text.startup+0xabc): undefined reference to
OsiClpSolverInterface::getColSolution() const' main-farmer.cpp:(.text.startup+0xae9): undefined reference to OsiClpSolverInterface::getColSolution() const'
main-farmer.cpp:(.text.startup+0xb16): undefined reference to
OsiClpSolverInterface::getColSolution() const' main-farmer.cpp:(.text.startup+0xb43): undefined reference to OsiClpSolverInterface::getColSolution() const'
main-farmer.o:main-farmer.cpp:(.text.startup+0xb70): more undefined
references toOsiClpSolverInterface::getColSolution() const' follow main-farmer.o: In function main':
main-farmer.cpp:(.text.startup+0xc8f): undefined reference to
CoinPackedMatrix::~CoinPackedMatrix()' main-farmer.cpp:(.text.startup+0xc9c): undefined reference to `OsiClpSolverInterface::~OsiClpSolverInterface()' collect2: error: ld returned 1 exit status
答案 0 :(得分:1)
要编译.c
和链接.o
文件,您应该编写
gcc FirstFile.o SecondFile.o ... -o main
但如果您编译.cpp
,请使用g++
代替gcc
。
外部库中的文件也应该写在上面的列表中。
您的命令输出了什么错误?