欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

linux开发工具 gcc gdb Makefile Cmake

程序员文章站 2022-07-02 09:02:54
...

作业

作业1 编译器三级优化分别优化了什么?

  • -O1 一级优化 提供基础级别的优化
    这一层通过代码调整 的方式优化,主要包括语句调整,用汇编重写、指令调整、换一种语言实现、换一个编译器、循环展开、参数传递优化等。代码调整是一种局部的思维方式,基本上不触及算法层级,它面向的是代码,而不是问题。
  • -O2 二级优化 提供更加高级的代码优化,会占用更长的编译时间
    这一级在上一级的基础上增加了新的视角,它主要强调的重点是针对问题的算法,即选择和构造适合于问题的算法,很多经典算法都对问题作了一些假设,而在面对实际问题时“新的视角”提示我们应该重新检视这些假设,并尝试不同的思考问题的角度,寻求适合于问题的新算法,发掘问题的本来意义,从不同的角度思考面对的问题,使用适合于问题的的算法; 尝试打破一些规则,发掘和怀疑自己的某些假定,恢复问题的本来面目;
  • -O3 三级优化 提供*的代码优化
    它整合了第一和第二级别中的优化技巧 。这一级可以将问题抽象为另一种等价的数学模型或假想机器模型,比如构造出某种表驱动状态机。这一级其实是第二级的延伸,只是产生的效果更加明显,但它有其本身的特点,任何算法和优化活动都可以看作是他的投影。这一级一般可以产生无与伦比的快速程序,第三级要求的思考优化问题的切入点,是寻找一部机器,使它运行经过的路径最短,可能是速度也可能是空间等。

作业2 总结gcc静态库和动态库的制作步骤

  • 静态库
    1.把源文件成目标文件 后缀为.o gcc add.c -c
    2.生成静态库文件 ar rcs 库名字(如libadd.a) add.o
    3.将主函数与静态库连接 gcc main.c -ladd -L. -o main -l链接 -L路径

  • 动态库
    gcc -shared -fPIC add.c -o libadd.so
    gcc main.c ./libadd.so -o main(可执行文件)

作业3 总结Cmake使用

例1:Hello World

1.写源文件helloworld.c 建文件CMakeList.txt 写此文件

[[email protected] cmakelearn]# vim helloworld.c
[[email protected] cmakelearn]# touch CMakeList.txt
[[email protected] cmakelearn]# ls
CMakeList.txt  helloworld.c
[[email protected] cmakelearn]# vim CMakeList.txt

  1 #include "stdio.h"
  2 
  3 int main()
  4 {
  5     printf("hello,world! Welcome! \n");
  6     return 0;
  7 }
  1 cmake_minimum_required(VERSION 2.8.9)
  2 project (hello)
  3 add_executable(hello helloworld.c)

2.用cmake生成Makefile文件

[[email protected] cmakelearn]# cmake .
-- The C compiler identification is GNU 4.4.7
-- The CXX compiler identification is GNU 4.4.7
-- 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
-- Detecting C compile features
-- Detecting C compile features - 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
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /root/0706/cmakelearn

[[email protected] cmakelearn]# ls
CMakeCache.txt  cmake_install.cmake  helloworld.c
CMakeFiles      CMakeLists.txt       Makefile

3.make编译程序 ,编译成功

[[email protected] cmakelearn]# make
Scanning dependencies of target hello
[ 50%] Building C object CMakeFiles/hello.dir/helloworld.c.o
[100%] Linking C executable hello
[100%] Built target hello

[[email protected] cmakelearn]# ./hello
hello,world! Welcome! 

例2: 包含目录结构的项目

1.写源文件add.c 头文件add.h 建文件CMakeLists.txt 写此文件

[[email protected] test2]# vim add.c
[[email protected] test2]# vim add.h
[[email protected] test2]# vim main.c
[[email protected] test2]# vim CmakeLists.txt
[[email protected] test2]# ls
add.c  add.h  main.c

add.c

  1 int add(int a,int b)
  2 {
  3     return a+b;
  4 }

add.h

 1 extern add(int a,int b);

CMakeLists.txt

  1 cmake_minimum_required(VERSION 2.8.9)
  2 project(directory_test)
  3 
  4 include_directories(include)
  5 
  6 file(GLOB SOURCES "/root/0706/cmakelearn/test2/*.c")
  7 
  8 add_executable(testAdd ${SOURCES})

2.用cmake生成Makefile文件

[[email protected] test2]# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /root/0706/cmakelearn/test2
[[email protected] test2]# ls
add.c  CMakeCache.txt  cmake_install.cmake  main.c    testAdd
add.h  CMakeFiles      CMakeLists.txt       Makefile

3.make编译程序 ,编译成功

[[email protected] test2]# make
Scanning dependencies of target testAdd
[ 33%] Building C object CMakeFiles/testAdd.dir/add.c.o
[ 66%] Building C object CMakeFiles/testAdd.dir/main.c.o
[100%] Linking C executable testAdd
[100%] Built target testAdd

[[email protected] test2]# ./testAdd
sum = 8 ! 

例3:动态库编译(.so)

1.写源文件sub.c 头文件sub.h 建文件CMakeLists.txt 写此文件
CMakeLists.txt :

  1 cmake_minimum_required(VERSION 2.8.9)
  2 project(directory_test)
  3 #set(CMAKE_BUILD_TYPE Release)
  4 
  5 #Bring the headers, such as Student.h into the project
  6 include_directories(include)
  7 
  8 #However, the file(GLOB...) allows for wildcard additions:
  9 file(GLOB SOURCES "/root/0706/cmakelearn/test3/*.c")
 10 
 11 #Generate the shared library from the sources
 12 add_library(testsub SHARED ${SOURCES})
 13 
 14 #Set the location for library installation -- i.e., /usr/lib in this case
 15 # not really necessary in this example. Use "sudo make install" to apply
 16 install(TARGETS testsub DESTINATION /usr/lib)

sub.c

  1 int sub(int a,int b)
  2 {
  3     return a-b;
  4 }

sub.h

  1 extern int sub(int a, int b);

2.用cmake生成Makefile文件

[[email protected] test3]# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /root/0706/cmakelearn/test3

3.make编译程序

[[email protected] test3]# make
Scanning dependencies of target testsub
[ 33%] Building C object CMakeFiles/testsub.dir/main.c.o
[ 66%] Building C object CMakeFiles/testsub.dir/sub.c.o
[100%] Linking C shared library libtestsub.so
[100%] Built target testsub

生成libtestsub.so

[[email protected] test3]# ls
CMakeCache.txt  cmake_install.cmake libtestsub.so  Makefile  sub.h
CMakeFiles      CMakeLists.txt       main.c         sub.c

例4:静态库编译 (.a)

只需将add_library中的 SHARED 改为 STATIC

  1 cmake_minimum_required(VERSION 2.8.9)
  2 project(directory_test)
  3 #set(CMAKE_BUILD_TYPE Release)
  4 
  5 #Bring the headers, such as Student.h into the project
  6 include_directories(include)
  7 
  8 #However, the file(GLOB...) allows for wildcard additions:
  9 file(GLOB SOURCES "/root/0706/cmakelearn/test3/*.c")
 10 
 11 #Generate the shared library from the sources
 12 add_library(testsub STATIC ${SOURCES})
 13 
 14 #Set the location for library installation -- i.e., /usr/lib in this case
 15 # not really necessary in this example. Use "sudo make install" to apply
 16 install(TARGETS testsub DESTINATION /usr/lib)

生成libtestsub.a

[[email protected] test3]# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /root/0706/cmakelearn/test3
[[email protected] test3]# make
Scanning dependencies of target testsub
[ 33%] Building C object CMakeFiles/testsub.dir/main.c.o
[ 66%] Building C object CMakeFiles/testsub.dir/sub.c.o
[100%] Linking C static library libtestsub.a
[100%] Built target testsub
[[email protected] test3]# ls
CMakeCache.txt  cmake_install.cmake  libtestsub.a   main.c    sub.c
CMakeFiles      CMakeLists.txt       libtestsub.so  Makefile  sub.h

在运行make install 之后,安装在了指定目录 /usr/lib/libtestsub.a

[[email protected] test3]# make install
-- Configuring done
-- Generating done
-- Build files have been written to: /root/0706/cmakelearn/test3
[100%] Built target testsub
Install the project...
-- Install configuration: ""
-- Installing: /usr/lib/libtestsub.a

例5:使用静态库或动态库

CMakeLists.txt 文件

  1 cmake_minimum_required(VERSION 2.8.9)
  2 project (TestLibrary)
  3 
  4 #For the shared library:
  5 set ( PROJECT_LINK_LIBS libtestsub.so )
  6 link_directories( ~/root/0706/cmakelearn/test3 )
  7 
  8 #For the static library:
  9 #set ( PROJECT_LINK_LIBS libtestStudent.a )
 10 #link_directories( ~/exploringBB/extras/cmake/test3 )
 11 
 12 include_directories(~/root/0706/cmakelearn/test3)
 13 
 14 add_executable(libsub sub.c)
 15 target_link_libraries(libsub ${PROJECT_LINK_LIBS} )
 16 

[[email protected] test3]# cmake .
-- Configuring done
-- Generating done
-- Build files have been written to: /root/0706/cmakelearn/test3

输出运行结果:

[[email protected] test3]# make
Scanning dependencies of target libsub
[ 50%] Building C object CMakeFiles/libsub.dir/sub.c.o
[100%] Linking C executable libsub
[100%] Built target libsub
[[email protected] test3]# ./libsub
sub = 5.
相关标签: Linux cmake