C语言网

 找回密码
 加入社区!

QQ登录

只需一步,快速开始

查看: 372|回复: 0

linux gcc make 文件结构 [复制链接]

Rank: 3Rank: 3

主题
0
帖子
174
C币
956 枚
在线时间
32 小时
发表于 2010-3-12 11:16:38 |显示全部楼层
1. 说明
1.1
这里演示怎么去建立一个 GCC 的工程文件体系,并能使用 gcc autotools
工具编译对应的文件,形成可执行的文件。我们以工程 hello 为例子.
1.2 注意
1.2.1  
目录和文件名都区分大小写,文件内容也区分.
1.2.2  
每一个目录下面对应只要一个 Makefile.am 作为配置文件
1.2.3  
全部配置只要一个 configure.in 文件
2. 目录和对应文件结构:
hello(dir)
    |---src(dir)
        |---hello.cpp(file)
        |Makefile.am(file)
    |---util(dir)
        |str.cpp(file)
        |str.h(file)
        |Makefile.am
    |configure.in(file)
    |Makefile.am(file)

3. 配置文件内容解释
3.1 hello/src/hello.cpp
内容,实现了程序入口
#include <iostream>
//
使用相对路径调用自定义头文件
#include "../util/str.h"
#include <iostream>
//
使用相对路径调用自定义头文件
#include "../util/str.h"
using namespace std;

int
main (void)
{
  std::cout << "Hello World !" << endl;
//
调用其他文件类
  CStr str;
  for (int i = 0; i < 5; i++)
    {
      str.Insert ("Item");
    }
  str.Pop ();
  std::cout << "Execute Successed !" << endl;
  std::cout << "aaaaaaaa" << endl;
  exit (EXIT_SUCCESS);
}

3.2 hello/src/Makefile.am 内容
AUTOMAKE_OPTIONS=foreign
bin_PROGRAMS=hello
#
假如要使用多个文件,请使用空格隔开,一般头文件
#
不要写,但是假如头文件包含实现,就需要写入
hello_SOURCES=hello.cpp
#
提供的包含路径,
INCLUDES= -I/usr/include/
#
假如包含多个库搜索路径,需要使用空格隔开.
# -L
是引入库的标志
LDFLAGS=-L../util -L/usr/lib/
#
假如包含多个库,需要使用空格隔开.
# -L
是引入库的标志
LIBS=-lutil

3.3 hello/util/str.h 内容,主要定义了调用的 CStr
#include <string>
#include <vector>

class CStr
{
private:
  std::vector<std::string> m_strs;
protected:
  //
public:
  void Insert(const std::string astr);
  void Pop();
};

3.4 hello/util/str.cpp 内容,主要实现了 CStr
#include <iostream>
#include "str.h"

using namespace std;

void
CStr::Insert (const std::string astr)
{
  m_strs.push_back (astr);
}


void
CStr:op ()
{
  for (std::vector < std::string >::iterator it = m_strs.begin ();
       it != m_strs.end (); it++)
    {
       std::cout<<*it<<endl;
    }
}

3.5 hello/util/Makefile.am 文件,定义了生成对应的 Makefile.in 的基本配置
AUTOMAKE_OPTIONS=foreign
#
编译为静态库文件
noinst_LIBRARIES=libutil.a
#
需要的源文件
libutil_a_SOURCES=str.cpp
#
参数
CFLAGS=-O2
CXXFLAGS=-O2


3.6 hello/Makefile.am
文件
AUTOMAKE_OPTIONS=foreign
#
定义需要两个目录
SUBDIRS=util src

3.7 hello/configure.in 文件
dnl Process this file with autoconf to produce a configure script.

AC_INIT(src/hello.cpp)
AM_INIT_AUTOMAKE(hello,1.0)

dnl Checks for programs.
AC_PROG_CC
AC_PROG_CXX

dnl Checks for libraries.
AC_PROG_RANLIB

dnl Checks for header files.
dnl Checks for typedefs, structures, and compiler characteristics.
dnl Checks for library functions.
dnl 定义需要检查两个子目录下面的 Makefile 文件
AC_OUTPUT(Makefile src/Makefile util/Makefile)

4. 生成执行文件 Makefile(全部命令在 hello 目录下执行)
4.1 acloacl
运行 aclocal,生成 aclocal.me autom4te.cache
4.2 autoconf
运行 autoconf,生成 configure 可执行文件
4.3 automake
运行 automake --add-missing 生成 depcomp,install-sh,missing
4.4 configure
运行 ./configure ,自动生成 Makefile
4.5 make
执行 make ,得到对应的自定义库文件和可执行的程序
4.5 执行
src/hello,
查看结果

5.
未了结问题
5.1
目标文件 .o 现在和源代码文件 .cpp 放在一起,考虑可以把所有的目标文件放在一个目录
5.2
可执行文件考虑也可以放在一个目录里面
5.3
其他目录的源文件是否可以不编译为库文件,比如 util 下面的所有文件
您需要登录后才可以回帖 登录 | 加入社区!

C语言 ( 粤ICP备11042647号-2 )

GMT+8, 2012-2-6 13:44

©2009-2011 cyuyan.com.cn

回顶部