当前位置:网站首页>How to use clion to debug a project built by the make tool

How to use clion to debug a project built by the make tool

2022-06-13 07:07:00 guangsu.

How to use Clion Debugging use make Projects built with tools

Background introduction

CLion Support usage only Cmake Tools build project , But there are many projects that use make Build ( such as php Core source code /redis Source code ).

So how to put a make Project import to CLion in , Turn into CMake How to build , So we can use CLion Read about some open source software , We used the familiar Ladybug to debug the breakpoint

review CMake And make The relationship between

make Is to help build ( Build is a general term for a series of operation processes of compiling and generating executable programs )C/C++ Project tools ,
It can parse makefile The content in , according to makefile Medium rule Determine the order of compilation , Dependency relationship ,
The same set of source code on different platforms makefile The content is different , So in Centos Upper makefile It may take a lot of changes before you can Ubuntu Upper use .CMake Is responsible for generating for different platforms makefile,
CMake Generate makefile Rely on a called CMakeLists.txt The file of , The contents of this file also control the compilation order / Dependent on rule.CMakeLists.txt It can be used across platforms , and makefile It can't be used across platforms .

development environment

[[email protected] ~]$ cat /etc/centos-release
CentOS Linux release 7.7.1908 (Core)
[[email protected] ~]$ gcc -v
gcc version 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
[[email protected] ~]$ gdb -v
GNU gdb (GDB) Red Hat Enterprise Linux 7.6.1-115.el7
[[email protected] ~]$ make -v
GNU Make 3.82

cmake  It's using CLion It comes with the kit 
CLion The version is  clion2020.1

Solution analysis ( With php Source code for example )

When not in use CLion Before , This is how we compile it

wget https://www.php.net/distributions/php-7.1.0.tar.gz
tar -zxvf php-7.1.0.tar.gz
cd php-7.1.0.tar.gz
#  Use official detection shell Script generation makefile
./configure --prefix=/home/sujianhui/php_debug/ --enable-fpm --enable-debug  
make 
make install 
#  Used in terminal gdb  Debugging the source code 
gdb /home/sujianhui/php_debug/bin/php 

My goal is simple , Just want to use CLion Breakpoint debugging php Kernel source code , Easy to read , Only with a purpose can we have a solution , There are probably two ways to solve this problem

  • contrast ./configure Command generated makefile, Backward generation CMakeLists.txt
  • Use ./configure Generated makefile, No need to use CMake Generate a new makefile

The first way of thinking , It will soon be pass fall , This will not only take a long time , And it's easy to go wrong , Do more harm than good
The second way of thinking , Need to put CMakeLists.txt In the middle of makefile Of this link rule Replace with php The source code comes with configure Detect script generation , It works in principle

Practice

1 CLion -> File -> New CMake Project From Source Open the unzipped source directory , This step CLion A... Will be created in the root directory of the new project CMakeLists.txt file ,cmake-build-debug Folder

2 vim CMakeLists.txt, Change to the following format

cmake_minimum_required(VERSION 3.16)
project(php_7_1_0)

set(CMAKE_CXX_STANDARD 14)

# Definition php Source path , Here you can change it according to your real path 
set(PHP_SOURCE /home/sujianhui/CLionProjects/php-7.1.0)
set(PHP_BIN_OUTPUT /home/sujianhui/CLionProjects/output/php7_1/)

# introduce php The required extended source code , This is also changed according to your own needs 
include_directories(${PHP_SOURCE}/main)
include_directories(${PHP_SOURCE}/Zend)
include_directories(${PHP_SOURCE}/sapi)
include_directories(${PHP_SOURCE}/pear)
include_directories(${PHP_SOURCE}/TSRM)
include_directories(${PHP_SOURCE}/ext)
include_directories(${PHP_SOURCE})

#  Be careful  --enable-debug  You have to add , Otherwise, breakpoint debugging will lack debugging symbol information 
add_custom_target(makefile COMMAND ./configure --prefix=${PHP_BIN_OUTPUT} --enable-fpm --enable-debug && make 
    	WORKING_DIRECTORY ${PHP_SOURCE})

add_custom_target(learn_extend COMMAND sudo ${PHP_BIN_OUTPUT}bin/phpize && ./configure --with-php-config=${PHP_BIN_OUTPUT}/bin/php-config &&  make
    	WORKING_DIRECTORY ${PHP_SOURCE})

In the above

add_custom_target(makefile COMMAND ./configure --prefix=${PHP_BIN_OUTPUT} --enable-fpm --enable-debug && make
             WORKING_DIRECTORY ${PHP_SOURCE})

What a command does is execute ./configure Script generation makefile, Replace the complex that should have appeared cmake rule, After configuring this step , You will find that the ladybug has lit up

3 Click on CLion The left side of the little Ladybug in the upper navigation bar Edit Configurations, Select... From the pop-up box CMake Applications Under the makefile target To configure

#  This parameter specifies when clicking the navigation bar  run( Trigonometry icon)  Button , Which executable to execute ,  Here I have chosen build Post generated php, amount to  gdb $(PHP_BIN_OUTPUT)/bin/php
Executable :  choice $(PHP_BIN_OUTPUT)/bin/php                       

#  This parameter is formulated when clicking on the navigation LAN  run  Button , Parameters passed into the executable , Equivalent to the gdb bash  In the implementation of  : r /home/sujianhui/php/1.php (1.php  It's a random one php file )
Programs Arguments : -f /home/sujianhui/php/1.php 

#  What is specified in this column is when clicking run After button , Before the real executable program is executed , Actions to be performed , Default added build operation , Each time run/debug Before , Will be forced to re Build once  , This configuration depends on your personal needs 
Before Launch :  Get rid of  Build 

4 CLion Open in sapi/cli/php_cli.c , stay main The beginning of the function ( I am here 1192 That's ok ) To set breakpoints , Click on the ladybug , If it's normal , Breakpoints are already in effect

Summary

It is normal to encounter various problems during configuration , If you need this , First you have to understand the idea of the solution , Then follow this idea to solve various problems in the configuration process .
For example, you may encounter Build Time multicore compilation warning The problem of …

CLion In the navigation column Build run Debug The difference between

  • Build : ./configure Generate makefile ,make Tool parsing makefile Compile , install , Until the binary executable file is generated
  • run : Execute it. Build Generated binary , For example, after we compile and install php After the source , We usually look at which modules are enabled , And then we'll be at shell In the implementation of php -m, This is it. run
  • Debug : Run a in debug mode to ,debug In essence, it detects child processes for the parent process ( Riru gdb Parent process ,gdb The process being debugged is a child process )

Debug Of Setting Path by : File -> Setting -> Build,Execution,Deployment -> CMake In the pop-up window , Here you can set some Debug Time parameters

Reference material

There are pictures in this one , There is something wrong with my local environment , There are no screenshots , For reference
https://blog.csdn.net/weixin_44056915/article/details/102859556

原网站

版权声明
本文为[guangsu.]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270550305200.html