文件结构牛

2021-08-24 淮安装修公司

1. 说明

1.1 这里演示怎么去建立一个 GCC 的工程文件体系,并能使用 gcc autotools

工具编译对应的文件,形成可执行的文件。我们以工程 hello 为例子.

1.2 注意

1.2.1 目录和文件名都区分大小写,文件内容也区分.

1.2.2 每一个目录下面对应只要一个 作为配置文件

1.2.3 全部配置只要一个 文件

2. 目录和对应文件结构:

hello(dir)

---src(dir)

p(file)

(file)

---util(dir)

p(file)

str.h(file)

(file)

(file)

3. 配置文件内容解释

3.1 hello/src/p 内容,实现了程序入口

#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++)

{

sert (\"Item\");

}

p ();

std::cout \"Execute Successed !\" endl;

std::cout \"aaaaaaaa\" endl;

exit (EXIT_SUCCESS);

}

3.2 hello/src/ 内容

AUTOMAKE_OPTIONS=foreign

bin_PROGRAMS=hello

# 假如要使用多个文件,请使用空格隔开,一般头文件

# 不要写,但是假如头文件包含实现,就需要写入

hello_SOURCES=p

#提供的包含路径,

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::vectorstd::string m_strs;

protected:

//

public:

void Insert(const std::string astr);

void Pop();

};

3.4 hello/util/p 内容,主要实现了 CStr 类

#include iostream

#include \"str.h\"

using namespace std;

void

CStr::Insert (const std::string astr)

{

m_sh_back (astr);

}

void

CStr::Pop ()

{

for (std::vector std::string ::iterator it = m_gin ();

it != m_d (); it++)

{

std::cout*itendl;

}

}

3.5 hello/util/ 文件,定义了生成对应的 的基本配置

AUTOMAKE_OPTIONS=foreign

#编译为静态库文件

noinst_LIBRARIES=libutil.a

#需要的源文件

libutil_a_SOURCES=p

# 参数

CFLAGS=-O2

CXXFLAGS=-O2

3.6 hello/ 文件

AUTOMAKE_OPTIONS=foreign

#定义需要两个目录

SUBDIRS=util src

3.7 hello/ 文件

dnl Process this file with autoconf to produce a configure script.

80后女孩变身女汉子 AC_INIT(src/p)

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,生成 和 che

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 下面的所有文件

永州治白癜风哪里最好
汕头看癫痫去哪
乌鲁木齐哪家白癜风医院好
为你推荐