GameServer工程SentOS系统编译

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Windgs_YF/article/details/88551878

问题汇总

1、cmake 报错

[root@iz build]# cmake ..
-- The C compiler identification is GNU 4.8.5
-- The CXX compiler identification is GNU 4.8.5
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:10 (add_compile_options):
  Unknown CMake command "add_compile_options".

解决办法:

在cmake脚本中,设置编译选项可以通过add_compile_options命令,也可以通过set命令修改CMAKE_CXX_FLAGS或CMAKE_C_FLAGS。 
使用这两种方式在有的情况下效果是一样的,但请注意它们还是有区别的: 
add_compile_options命令添加的编译选项是针对所有编译器的(包括c和c++编译器),而set命令设置CMAKE_C_FLAGS或CMAKE_CXX_FLAGS变量则是分别只针对c和c++编译器的。

例如下面的代码

#判断编译器类型,如果是gcc编译器,则在编译选项中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)
    add_compile_options(-std=c++11)
    message(STATUS "optional:-std=c++11")   
endif(CMAKE_COMPILER_IS_GNUCXX)

使用add_compile_options添加-std=c++11选项,是想在编译c++代码时加上c++11支持选项。但是因为add_compile_options是针对所有类型编译器的,所以在编译c代码时,就会产生如下warning

J:\workspace\facecl.gcc>make b64 
[ 50%] Building C object libb64/CMakeFiles/b64.dir/libb64-1.2.1/src/cdecode.c.obj 
cc1.exe: warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C 
[100%] Building C object libb64/CMakeFiles/b64.dir/libb64-1.2.1/src/cencode.c.obj 
cc1.exe: warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C 
Linking C static library libb64.a 
[100%] Built target b64
虽然并不影响编译,但看着的确是不爽啊,要消除这个warning,就不能使用add_compile_options,而是只针对c++编译器添加这个option。 
所以如下修改代码,则警告消除。

#判断编译器类型,如果是gcc编译器,则在编译选项中加入c++11支持
if(CMAKE_COMPILER_IS_GNUCXX)
    set(CMAKE_CXX_FLAGS "-std=c++11 ${CMAKE_CXX_FLAGS}")
    message(STATUS "optional:-std=c++11")   
endif(CMAKE_COMPILER_IS_GNUCXX)

举一反三,我们就可以想到,add_definitions这个命令也是同样针对所有编译器,一样注意这个区别。

1、编译时提示头文件#include <mysql/mysql.h>不存在

解决办法:

$ sudo yum install mysql-devel -y    //RHEL,Centos,Fedora
$ sudo apt-get install libmysqlclient-dev -y  //Ubuntu

如果已经安装成功了,找到mysql.h的文件路径,-I 编译即可

$ sudo  find /usr/ -name 'mysql.h'
$ gcc -I/usr/include/mysql ...

我是通过设置环境变量来解决的(第二种方法)

在 Linux CentOS 系统上安装完 MATLAB 后,为了使用方便,需要将 matlab 命令加到系统命令中,如果在没有添加到环境变量之前,执行“matlab”命令时,则会提示命令不存在的错误,如下所示:



下面我详细介绍一下在 linux 下将 MATLAB 加入到环境变量中的方法(MATLAB 安装在 /usr/local/MATLAB/R2013a/bin 目录下)。

方法一(暂时生效)

直接运行命令export PATH=$PATH:/usr/local/MATLAB/R2013a/bin ,使用这种方法,只会对当前回话生效,也就是说每当登出或注销系统以后,PATH 设置就会失效,只是临时生效。

方法二(只对当前登陆用户生效,永久生效)

执行 vim ~/.bash_profile 修改文件中 PATH 一行,将 /usr/local/MATLAB/R2013a/bin 加入到 PATH=$PATH:$HOME/bin 一行之后(注意以冒号分隔),保存文件并退出,执行 source ~/.bash_profile 使其生效,这种方法只对当前登陆用户生效。



看到如下图便可知环境变量加入成功:



方法三(对所有系统用户生效,永久生效)

修改 /etc/profile 文件,在文件末尾加上如下两行代码 
PATH=$PATH:/usr/local/MATLAB/R2013a/bin 
export PATH

最后执行命令 source /etc/profile 或执行点命令 ./profile 使其修改生效。

以上便是自己总结的三个在 linux 下添加环境变量的方法,可根据需求进行选择,事半功倍。

3、提示文件错误

/usr/local/bin/cmake -E cmake_progress_report /home/GameServer_trunk/build/CMakeFiles 28
[ 18%] Building CXX object CMakeFiles/gameserver.dir/Main/Player.cpp.o
/usr/bin/c++    -std=c++11   -O0 -Wall -g -ggdb -I/home/GameServer_trunk/Base -I/home/GameServer_yunying_trunk/Common -I/home/GameServer_trunk/Main -I/home/GameServer_yunying_trunk/Main/BaseObj -I/home/GameServer_yunying_trunk/Main/Battle -I/home/GameServer_trunk/Main/Friend -I/home/GameServer_yunying_trunk/Main/Item -I/home/GameServer_yunying_trunk/Main/LimitAward -I/home/GameServer_yunying_trunk/Main/OnLineAward -I/home/GameServer_yunying_trunk/Main/SignUp -I/home/GameServer_yunying_trunk/Main/TaskDetail -I/home/GameServer_yunying_trunk/Main/TopList -I/home/GameServer_trunk/NetBase -I/home/GameServer_yunying_trunk/Protocol -I/home/GameServer_trunk/WorldData -I/home/GameServer_yunying_trunk/MsgHandler -I/home/GameServer_trunk/DataBase -I/home/GameServer_yunying_trunk/WebSocket    -o CMakeFiles/gameserver.dir/Main/Player.cpp.o -c /home/GameServer_yunying_trunk/Main/Player.cpp
/home/GameServer_yunying_trunk/Main/Player.cpp:20:21: fatal error: stropts.h: No such file or directory
 #include <stropts.h>
                     ^
compilation terminated.
make[2]: *** [CMakeFiles/gameserver.dir/Main/Player.cpp.o] Error 1
make[2]: Leaving directory `/home/GameServer_trunk/build'
make[1]: *** [CMakeFiles/gameserver.dir/all] Error 2
make[1]: Leaving directory `/home/GameServer_trunk/build'
make: *** [all] Error 2

解决办法:

出现这个问题一般是在centos 操作系统上,Ubuntu上默认是有这个文件的。   

头文件stropts.h是POSIX XSR的一部分,因为linux不支持STREAMS,所以缺少这个文件。
解决办法很简单,在/usr/include目录下创建一个空的stropts.h文件即可。

最后成功在sentos上编译通过

make[2]: Leaving directory `/home/GameServer_trunk/build'
/usr/local/bin/cmake -E cmake_progress_report /home/GameServer_trunk/build/CMakeFiles  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
[100%] Built target gameserver
make[1]: Leaving directory `/home/GameServer_trunk/build'
/usr/local/bin/cmake -E cmake_progress_start /home/GameServer_trunk/build/CMakeFiles 0
[root@izbpy9z build]# 

猜你喜欢

转载自blog.csdn.net/Windgs_YF/article/details/88551878
今日推荐