1

我正在尝试在 Linux 下构建 C++ 代码。这是我的第一次尝试(否则,我只使用 Windows)。我的代码正在使用 MySQL C API 库,但我在构建它时遇到了问题。我在尝试构建 MainProgram 时得到了这个输出

g++ -lstdc++ -c  MainProgram.cpp `mysql_config -–libs` -O2 -o MainProgram.obj `mysql_config –-cflags`

   g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
   Copyright (C) 2006 Free Software Foundation, Inc.
   This is free software; see the source for copying conditions.  There is NO
   warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

然后链接没有完成,因为缺少 MainProgram.obj

我的makefile是这样的:

CC = g++ CFLAGS = -lm -lstdc++  -Wall -O2  

MYSQLCFLAGS = `mysql_config –-cflags` 
MYSQLLIBS   = `mysql_config -–libs`

BIN = my_program


OBJ = BlockingSocket.obj FTPClient.obj FTPDataTypes.obj FTPFileStatus.obj FTPListParse.obj MyStringAnsi.obj MainProgram.obj 



.PHONY: sestav .PHONY: clean


#-----------------------------------------

clean:  rm -f *.obj


#----------------------------------------- 

sestav: ${BIN}


#-----------------------------------------

$(BIN): $(OBJ) $(OBJ)   $(CC) $(CFLAGS) $^ -o $@


#-----------------------------------------

BlockingSocket.obj: ./FtpClient/BlockingSocket.cpp  $(CC) -c ./FtpClient/BlockingSocket.cpp -O2 -o BlockingSocket.obj

#-------

FTPClient.obj: ./FtpClient/FTPClient.cpp    
  $(CC) -c ./FtpClient/FTPClient.cpp -O2 -o FTPClient.obj

FTPDataTypes.obj: ./FtpClient/FTPDataTypes.cpp  
  $(CC) -c ./FtpClient/FTPDataTypes.cpp -O2 -o FTPDataTypes.obj

FTPFileStatus.obj: ./FtpClient/FTPFileStatus.cpp    
  $(CC) -c ./FtpClient/FTPFileStatus.cpp -O2 -o FTPFileStatus.obj    

FTPListParse.obj: ./FtpClient/FTPListParse.cpp  
  $(CC) -c ./FtpClient/FTPListParse.cpp -O2 -o FTPListParse.obj

MyStringAnsi.obj: MyStringAnsi.cpp  
  $(CC) -c MyStringAnsi.cpp -O2 -o MyStringAnsi.obj


MainProgram.obj: MainProgram.cpp   
  $(CC) -lstdc++ -c  MainProgram.cpp $(MYSQLLIBS) -O2 -o MainProgram.obj $(MYSQLCFLAGS)

从头开始构建时的完整输出:

g++ -c ./FtpClient/BlockingSocket.cpp -O2 -o BlockingSocket.obj
g++ -c ./FtpClient/FTPClient.cpp -O2 -o FTPClient.obj
g++ -c ./FtpClient/FTPDataTypes.cpp -O2 -o FTPDataTypes.obj
g++ -c ./FtpClient/FTPFileStatus.cpp -O2 -o FTPFileStatus.obj
./FtpClient/FTPFileStatus.cpp:136:2: warning: no newline at end of file
g++ -c ./FtpClient/FTPListParse.cpp -O2 -o FTPListParse.obj
g++ -c MyStringAnsi.cpp -O2 -o MyStringAnsi.obj
In file included from MyStringAnsi.cpp:1:
MyString.h:257:7: warning: no newline at end of file
In file included from MyStringAnsi.cpp:3:
./Macros.h:22:7: warning: no newline at end of file
MyStringAnsi.cpp:1350:2: warning: no newline at end of file
MyStringAnsi.cpp: In member function â€void MyStringAnsi::operator+=(char)’:
MyStringAnsi.cpp:1102: warning: NULL used in arithmetic
g++ -lstdc++ -c  MainProgram.cpp `mysql_config -–libs` -O2 -o MainProgram.obj `mysql_config –-cflags`       
g++ (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ -lm -lstdc++  -Wall -O2   BlockingSocket.obj FTPClient.obj FTPDataTypes.obj FTPFileStatus.obj FTPListParse.obj MyStringAnsi.obj MainProgram.obj -o my_program
g++: MainProgram.obj: No such file or directory
make: *** [MainProgram] Error 1
4

1 回答 1

1

好的..我解决了这个问题。首先,正如 Joachim 在评论中建议的那样,我从构建中删除了库到对象其次,我没有使用 mysql_config --libs,而是手动放置了路径

现在makefile编辑如下:

CFLAGS = -lm -lstdc++  -Wall -O2 -L/usr/lib/mysql/ -lmysqlclient

$(CC) -c  MainProgram.cpp -O2 -o MainProgram.obj -I/usr/include/mysql/  
于 2013-04-25T10:07:35.333 回答