程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> linux 下cmake 編譯 ,調用,調試 poco 1.6.0 小記,cmake1.6.0

linux 下cmake 編譯 ,調用,調試 poco 1.6.0 小記,cmake1.6.0

編輯:C++入門知識

linux 下cmake 編譯 ,調用,調試 poco 1.6.0 小記,cmake1.6.0


上篇文章 小記了:

關於 Poco::TCPServer框架 (windows 下使用的是 select模型) 學習筆記.

http://www.cnblogs.com/bleachli/p/4352959.html

這兒繼續學習下,poco在linux 下用cmake 編譯.

 

 

從文檔開始編譯出錯

從poco的 README 文件上可知:

"

BUILDING ON UNIX/LINUX/MAC OS X
===============================

For building on Unix platforms, the POCO C++ Libraries come with their own
build system. The build system is based on GNU Make 3.80 (or newer), with the help 
from a few shell scripts. If you do not have GNU Make 3.80 (or later) installed on 
your machine, you will need to download it from 
http://directory.fsf.org/devel/build/make.html>, 
build and install it prior to building the POCO C++ Libraries.

You can check the version of GNU Make installed on your system with

> gmake --version

or

> make --version

Once you have GNU Make up and running, the rest is quite simple.
To extract the sources and build all libraries, testsuites and samples, simply

> gunzip poco-X.Y.tar.gz
> tar -xf poco-X.Y.tar
> cd poco-X.Y
> ./configure
> gmake -s

"

但一直覺得 cmake 比較簡單,所以用cmake 編譯.官網就命令就兩句:

"

cmake .

make 

"

 

照著官網的說明編譯poco時,會出錯.

http://pocoproject.org/docs/00200-GettingStarted.html

Building On Unix/Linux/Mac OS X

可能出現的錯誤:

1,錯誤,cmake_minimum_required(VERSION 3.0.0) 

cmake 最小版本需要3.0.0以上.

 

2,錯誤make[2]: *** No rule to make target `lib/libPocoFoundation.so.1 (2014-12-22).6 (2014-12-22).0 (2014-12-22)', needed by `lib/libPocoXML.so.30'. Stop.
make[1]: *** [XML/CMakeFiles/XML.dir/all] Error 2
make: *** [all] Error 2

poco/version 文件

1.6.0 (2014-12-22)->有空格 ,,,生成 的so 文件最後有問題,不能調用.

 

改為

1.6.0

 

否則生成 so的名字中有空格,,,不能正確的生成 文件.

生成poco   so庫

在poco中執行

cmake . 

make 

就會看見:

[ 34%] Built target Foundation
[ 47%] Built target XML
[ 49%] Built target JSON
[ 68%] Built target Net
[ 72%] Built target MongoDB
[ 77%] Built target Util
[ 79%] Built target Crypto
[ 84%] Built target NetSSL
[ 91%] Built target Data
[ 92%] Built target DataSQLite
[ 94%] Built target DataMySQL
[ 99%] Built target Zip
[100%] Built target PageCompiler
[100%] Built target File2Page

 

 

使用poco庫

這就是我們所需要的東西.

那怎麼使用呢.

上次提到過

關於 Poco::TCPServer框架 (windows 下使用的是 select模型) 學習筆記.

http://www.cnblogs.com/bleachli/p/4352959.html

 TimeServer.cpp 的工程在

...\poco-1.6.0-all\Net\samples\TimeServer 中

看下TimeServer 目錄下,自帶的CMakeLists.txt 

set(SAMPLE_NAME "TimeServer")

set(LOCAL_SRCS "")
aux_source_directory(src LOCAL_SRCS)

add_executable( ${SAMPLE_NAME} ${LOCAL_SRCS} )
target_link_libraries( ${SAMPLE_NAME} PocoNet PocoUtil PocoJSON PocoXML PocoFoundation )

但這個不能編譯,會報,少了include 頭文件,以及找不到so文件.

錯誤信息:

[root@localhost TimeServer]# /home/cmake-3.1.3-Linux-i386/bin/cmake  .
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.1)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /home/poco-1.6.0-all/Net/samples/TimeServer
[root@localhost TimeServer]# make
Scanning dependencies of target TimeServer
[100%] Building CXX object CMakeFiles/TimeServer.dir/src/TimeServer.o
/home/poco-1.6.0-all/Net/samples/TimeServer/src/TimeServer.cpp:15:32: fatal error: Poco/Net/TCPServer.h: No such file or directory
compilation terminated.
make[2]: *** [CMakeFiles/TimeServer.dir/src/TimeServer.o] Error 1
make[1]: *** [CMakeFiles/TimeServer.dir/all] Error 2
make: *** [all] Error 2
[root@localhost TimeServer]# 

 

所以改改.

#定義最低版本.
cmake_minimum_required(VERSION 3.0.0) 

# 
set(SAMPLE_NAME "TimeServer")

PROJECT(${SAMPLE_NAME})        #定義工程名稱

SET(CMAKE_BUILE_TYPE DEBUG)        #指定編譯類型 設置編譯類型debug 或者release。 debug 版會生成相關調試信息,可以使用GDB 進行 

#SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")

 #SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")


#ADD_SUBDIRECTORY(utility) #添加要編譯的子目錄 為工程主目錄下的存放源代碼的子目錄使用該命令,各子目錄出現的順序隨意。
# 
set(path_root_dir "/home/poco-1.6.0-all")

# 添加頭文件搜索路徑
include_directories(${path_root_dir}/Net/include)
include_directories(${path_root_dir}/Util/include)
include_directories(${path_root_dir}/JSON/include)
include_directories(${path_root_dir}/XML/include)
include_directories(${path_root_dir}/Foundation/include)
#include_directories(${path_root_dir}lib/)


# 添加非標准的共享庫搜索路徑
LINK_DIRECTORIES(${path_root_dir}/lib/)


set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${path_root_dir}/lib)
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${path_root_dir}/lib)
# Windows DLLs are "runtime" for CMake. Output them to "bin" like the Visual Studio projects do.
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${path_root_dir}/bin)
#設置程序exe輸出位置
#SET(EXECUTABLE_OUTPUT_PATH ${path_root_dir}/bin)


MESSAGE(STATUS "t1 This is SOURCE dir ${path_root_dir}/Net")
MESSAGE(STATUS "t1 This is SOURCE dir ${path_root_dir}/bin")

# 
set(LOCAL_SRCS "")

# aux_source_directory 作用是發現一個目錄下所有的源代碼文件並將列表存儲在一個變量中,這個指令臨時被用來
# 自動構建源文件列表。因為目前cmake還不能自動發現新添加的源文件。
aux_source_directory(src LOCAL_SRCS)

# 生成exe  名字為 ${SAMPLE_NAME} 的值
add_executable(${SAMPLE_NAME} ${LOCAL_SRCS} )
# 這個指令可以用來為target添加需要鏈接的共享庫
target_link_libraries( ${SAMPLE_NAME} PocoNet PocoUtil PocoJSON PocoXML PocoFoundation )

 

再運行:

[root@localhost TimeServer]# /home/cmake-3.1.3-Linux-i386/bin/cmake  .
-- t1 This is SOURCE dir /home/poco-1.6.0-all/Net
-- t1 This is SOURCE dir /home/poco-1.6.0-all/bin
-- Configuring done
-- Generating done
-- Build files have been written to: /home/poco-1.6.0-all/Net/samples/TimeServer
[root@localhost TimeServer]# make
-- t1 This is SOURCE dir /home/poco-1.6.0-all/Net
-- t1 This is SOURCE dir /home/poco-1.6.0-all/bin
-- Configuring done
-- Generating done
-- Build files have been written to: /home/poco-1.6.0-all/Net/samples/TimeServer
[100%] Built target TimeServer
[root@localhost TimeServer]# ^C
[root@localhost TimeServer]# 

生成 TimeServer程序.

 

 

 

[root@localhost ~]# netstat  -nlap | grep Time
tcp        0      0 0.0.0.0:9911            0.0.0.0:*               LISTEN      29518/./TimeServer  

 用gdb 調試 TimeServer

  關於調試,網上說的很多不能調試.

  gdb使用可參考:

  http://blog.csdn.net/bobocheng1231/article/details/2513741

[root@localhost bin]# gdb TimeServer
GNU gdb (GDB) Fedora (7.5.0.20120926-25.fc18)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law. Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/poco-1.6.0-all/bin/TimeServer...(no debugging symbols found)...done.
(gdb) info b
No breakpoints or watchpoints.

#設置斷點
(gdb) b TimeServerConnection::run
Breakpoint 1 at 0x804be43

#運行
(gdb) r
Starting program: /home/poco-1.6.0-all/bin/TimeServer
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/libthread_db.so.1".
[New Thread 0xb7babb40 (LWP 17365)]
[New Thread 0xb73aab40 (LWP 17366)]
[New Thread 0xb6ba9b40 (LWP 17367)]
[Switching to Thread 0xb7babb40 (LWP 17365)]

Breakpoint 1, 0x0804be43 in TimeServerConnection::run() ()
Missing separate debuginfos, use: debuginfo-install glibc-2.16-24.fc18.i686 libgcc-4.7.2-8.fc18.i686 libstdc++-4.7.2-8.fc18.i686

#顯示 信息
(gdb) l
1 <built-in>: No such file or directory.
(gdb) l
1 in <built-in>
(gdb)

結果 不能顯示 出信息.

糾結許久.google之解決 問題.

原文:

http://www.cmake.org/pipermail/cmake/2012-September/052071.html

Stupid, stupid me. Yes it works, I just ran GDB with "start" instead of 
"run".
Thanks for your help Nils.

Chris


On 2012/09/19 04:56 PM, Nils Gladitz wrote:
> "cmake -DCMAKE_BUILD_TYPE=Debug" should be enough to get gdb 
> debuggable binaries.
> Is the process which you intend to debug running when you ask gdb for 
> a backtrace?
> You will only be able to get a backtrace if the process has been 
> started and has not yet exited.
>
> e.g.:
> gdb ./mybin
> break main
> run
> backtrace
>
> Nils
>
> On 09/19/2012 04:43 PM, GOO Creations wrote:
>> Hi,
>>
>> I'm trying to debug my program using GDB. I've done the following:
>>
>> -DCMAKE_BUILD_TYPE=Debug
>> SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fPIC -g")
>> SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g")
>>
>> But whenever I run GDB and do a backtrace, I get a message: "No Stack".
>> I've done a Google search and apparently the -g flag is never passed 
>> to g++.
>> Is -g automatically added when my build type is "Debug"? Does anyone 
>> know why GDB gives me this error?
>>
>> Thanks
>> Chris
>> -- 
>>
>> Powered by www.kitware.com

使用start 代替run 命令.

於試再試試

[root@localhost bin]# gdb TimeServer 
GNU gdb (GDB) Fedora (7.5.0.20120926-25.fc18)
Copyright (C) 2012 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i686-redhat-linux-gnu".
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>...
Reading symbols from /home/poco-1.6.0-all/bin/TimeServer...(no debugging symbols found)...done.
#設置斷點 (gdb) b TimeServerConnection::run Breakpoint 1 at 0x804be43
#斷點信息 (gdb) info b Num Type Disp Enb Address What 1 breakpoint keep y 0x0804be43 <TimeServerConnection::run()+5>
#查看源碼
(gdb) l No symbol table is loaded. Use the "file" command.
#開始 (gdb) start Temporary breakpoint 2 at 0x804b924 Starting program: /home/poco-1.6.0-all/bin/TimeServer [Thread debugging using libthread_db enabled] Using host libthread_db library "/lib/libthread_db.so.1". Temporary breakpoint 2, 0x0804b924 in main () Missing separate debuginfos, use: debuginfo-install glibc-2.16-24.fc18.i686 libgcc-4.7.2-8.fc18.i686 libstdc++-4.7.2-8.fc18.i686 (gdb) l 1 // 2 // TextConverter.cpp 3 // 4 // $Id: //poco/1.4/Foundation/src/TextConverter.cpp#1 $ 5 // 6 // Library: Foundation 7 // Package: Text 8 // Module: TextConverter 9 // 10 // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. (gdb) c Continuing. [New Thread 0xb7babb40 (LWP 17405)] [New Thread 0xb73aab40 (LWP 17406)] [New Thread 0xb6ba9b40 (LWP 17407)] [Switching to Thread 0xb7babb40 (LWP 17405)] Breakpoint 1, 0x0804be43 in TimeServerConnection::run() ()
#源碼信息..... (gdb) l 11 // and Contributors. 12 // 13 // SPDX-License-Identifier: BSL-1.0 14 // 15 16 17 #include "Poco/TextConverter.h" 18 #include "Poco/TextIterator.h" 19 #include "Poco/TextEncoding.h" 20 (gdb) n Single stepping until exit from function _ZN20TimeServerConnection3runEv, which has no line number information. Request from 192.168.10.220:13986 Poco::Net::TCPServerConnection::start (this=0xb6000468) at /home/poco-1.6.0-all/Net/src/TCPServerConnection.cpp:59 59 } (gdb) l 54 } 55 catch (...) 56 { 57 ErrorHandler::handle(); 58 } 59 } 60 61 62 } } // namespace Poco::Net (gdb) n Poco::Net::TCPServerDispatcher::run (this=0x8066308) at /home/poco-1.6.0-all/Net/src/TCPServerDispatcher.cpp:117 117 endConnection(); (gdb) l 112 { 113 std::auto_ptr<TCPServerConnection> pConnection(_pConnectionFactory->createConnection(pCNf->socket())); 114 poco_check_ptr(pConnection.get()); 115 beginConnection(); 116 pConnection->start(); 117 endConnection(); 118 } 119 } 120 121 FastMutex::ScopedLock lock(_mutex); (gdb) n 121 FastMutex::ScopedLock lock(_mutex); (gdb) n 122 if (_stopped || (_currentThreads > 1 && _queue.empty())) (gdb) l 117 endConnection(); 118 } 119 } 120 121 FastMutex::ScopedLock lock(_mutex); 122 if (_stopped || (_currentThreads > 1 && _queue.empty())) 123 { 124 --_currentThreads; 125 break; 126 } (gdb) c Continuing. Breakpoint 1, 0x0804be43 in TimeServerConnection::run() () (gdb)

這樣就可以調試了.

 

文章中如有錯誤,歡迎朋友們指點,謝謝.

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved