Category Archives: Linux编程

cmake检测编译环境

cmake检测编译环境

cmake检测编译环境就是检测当前编译工程的环境是否满足工程的需要,这包括

1. 编译器的版本,如gcc

2. cmake的版本

3. 需要的一些库

前两者不在这里叙述,比较简单,这里说说如何检测第三个。检测第三个有3个方法,

a)使用find_package

继续阅读

gcc 禁止warning

gcc 禁止warning

熟悉windows编程的人都知道,禁止编译器输出某个warning,在代码中可以这样

#pragma warning( disable 号码)

例如

#pragma warning(disable 4096)

这里的4096是warning的号码,

在gcc中warning是直接使用名字代表,而不是数字,例如 未使用的变量的 warning就是

继续阅读

Linux查找动态库的3种方法

Linux查找动态库的3种方法

linux默认加载动态库的路径是 /lib或者是/lib64, 32位的系统是前者,64位是后者。

如果找不到还有3个地方

  1. LD_LIBRARY_PATH
  2. rpath
  3. ldconfig

通过设置LD_LIBRARY_PATH环境变量方式,让系统在这个环境变量设置的路径查找动态库。

$> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:XXXXXX

继续阅读

CMake之RPath

CMake之RPath

其实rpath并不是定义或者发明的,这个东西是linux的东西,为什么把它和CMake放在一起讨论呢?是因为鄙人之前一直用手写Makefile来管理和编译工程,但是如今使用CMake,结果发现编译出来的可执行文件的链接到绝对路径的动态库,这样可执行文件就不可以拷贝到别的机器执行了,而之前使用makefile就没有这个问题。

继续阅读

CMake的CPack

CMake的CPack

CMake不仅可以用来管理工程,编译,安装,还可以制作安装包,安装包可以是压缩包还可以是rpm包。

下面利用一个简单的例子来解释如何使用CMake的cpack制作安装包

工程名 = Speaker

目录结构

Speaker +

              — CMakeLists.txt                ———> 1

继续阅读

RPM的安装,卸载,更新和查询

RPM的安装,卸载,更新和查询

下面的文字是摘自:

RPM Command: 15 Examples to Install, Uninstall, Upgrade, Query RPM Packages

非常全面的用例子的方式解释各个rpm的命令参数,懒得翻译了,就直接上原文了。

RPM command is used for installing, uninstalling, upgrading, querying, listing, and checking RPM packages on your Linux system.

RPM stands for Red Hat ackage Manager.

With root privilege, you can use the rpm command with appropriate options to manage the RPM software packages.

In this article, let us review 15 practical examples of rpm command.

Let us take an rpm of Mysql Client and run through all our examples.

1. Installing a RPM package Using rpm -ivh

RPM filename has packagename, version, release and architecture name.

For example, In the MySQL-client-3.23.57-1.i386.rpm file:

  • MySQL-client – Package Name
  • 3.23.57 – Version
  • 1 – Release
  • i386 – Architecture

When you install a RPM, it checks whether your system is suitable for the software the RPM package contains, figures out where to install the files located inside the rpm package, installs them on your system, and adds that piece of software into its database of installed RPM packages.

The following rpm command installs Mysql client package.

# rpm -ivh  MySQL-client-3.23.57-1.i386.rpm
Preparing...                ########################################### [100%]
   1:MySQL-client           ########################################### [100%]

rpm command and options

  • -i : install a package
  • -v : verbose
  • -h : print hash marks as the package archive is unpacked.

You can also use dpkg on Debian, pkgadd on Solaris, depot on HP-UX to install packages.

2. Query all the RPM Packages using rpm -qa

You can use rpm command to query all the packages installed in your system.

# rpm -qa
cdrecord-2.01-10.7.el5
bluez-libs-3.7-1.1
setarch-2.0-1.1
.
.
  • -q query operation
  • -a queries all installed packages

To identify whether a particular rpm package is installed on your system, combine rpm and grep command as shown below. Following command checks whether cdrecord package is installed on your system.

# rpm -qa | grep 'cdrecord'

继续阅读

CMake的install

CMake的install

CMake不止可以用来管理工程,编译工程,还可以安装工程。就是用install命令,但是记住他是cmake的命令,所以不要直接把它当作是一个linux的命令,那样就和linux的install命令混淆了,linux自带一个install命令,它和cp相类似。cmake的install命令可以按照下面的方式使用:

继续阅读

CMake完整示例

 CMake完整示例

以下文字全部摘抄在cmake的官网

CMake Tutorial

————————————————–

Below is a step-by-step tutorial covering common build system issues that CMake helps to address. Many of these topics have been introduced in Mastering CMake as separate issues but seeing how they all work together in an example project can be very helpful. This tutorial can be found in the Tests/Tutorial directory of the CMake source code tree. Each step has its own subdirectory containing a complete copy of the tutorial for that step

A Basic Starting Point (Step1)

The most basic project is an executable built from source code files. For simple projects a two line CMakeLists file is all that is required. This will be the starting point for our tutorial. The CMakeLists file looks like:

cmake_minimum_required (VERSION 2.6)
project (Tutorial)
add_executable(Tutorial tutorial.cxx)

Note that this example uses lower case commands in the CMakeLists file. Upper, lower, and mixed case commands are supported by CMake. The source code for tutorial.cxx will compute the square root of a number and the first version of it is very simple, as follows:

// A simple program that computes the square root of a number
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main (int argc, char *argv[])
{
  if (argc < 2)
    {
    fprintf(stdout,"Usage: %s number\n",argv[0]);
    return 1;
    }
  double inputValue = atof(argv[1]);
  double outputValue = sqrt(inputValue);
  fprintf(stdout,"The square root of %g is %g\n",
          inputValue, outputValue);
  return 0;
}

继续阅读

为代码优化提供证据和代码覆盖度

为代码优化提供证据和代码覆盖度

2个工具联合起来实现这一功能:

  • gcov
  • lcov

第一个是根据代码生成结果

第二个工具根据第一个工具产生的结果生成可视化的结果。

下载和说明

gcov:

https://gcc.gnu.org/onlinedocs/gcc/Gcov.html

lcov:

http://ltp.sourceforge.net/coverage/lcov.php

使用方法

假设你有代码, main.cpp

继续阅读

linux core dump文件的分析

Core,又称之为Core
Dump文件,是Unix/Linux操作系统的一种机制,对于线上服务而言,Core令人闻之色变,因为出Core的过程意味着服务暂时不能正常响应,需要恢复,并且随着吐Core进程的内存空间越大,此过程可能持续很长一段时间(例如当进程占用60G+以上内存时,完整Core文件需要15分钟才能完全写到磁盘上),这期间产生的流量损失,不可估量。

继续阅读