C ++程序(.cpp)如何与标头(.h)和libtool(.la)一起使用? [英] How a c++ program (.cpp) work with header(.h) and libtool (.la)?

查看:203
本文介绍了C ++程序(.cpp)如何与标头(.h)和libtool(.la)一起使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Linux中创建了一个名为helloworld的文件夹. 在此文件夹内有子目录:

I have create a folder in linux called helloworld. Inside this folder have sub-directory:

  • 包括
  • lib
  • src

include/ 在此目录中,我创建了一个名为helloworld.h的头文件. 内容是:

include/ In this directory I have create a header file called helloworld.h content are:

class helloworld{
public:

       void getvalue();

};

lib/ 在lib目录中,我创建了一个名为helloworld.cpp的cpp文件. 内容主要是功能:

lib/ In lib directory I have create a cpp file called helloworld.cpp content are mainly functions:

#include<iostream>
#include<helloworld.h>
using namespace std;

void helloworld::getvalue() {

}

src/ 在src目录中,我创建了一个名为main.cpp的主cpp文件. 内容是主要的:

src/ In src directory I have create a main cpp file called main.cpp content are main:

#include<iostream>
#include<helloworld.h>

int main()
{
helloworld a;
a.getvalue();
}

但是在我执行autoconf,automake,./configure以及何时进行make之后 它有一个错误:

but after I autoconf, automake, ./configure, and when make it has a error:

helloworld/src/main.cpp:8: undefined reference to `helloworld::getvalue()'

我只想在main.cpp中使用helloworld.cpp的函数.我花了很多时间进行搜索并尝试并出错.请帮忙.

All I want is to use helloworld.cpp's functions in main.cpp. I've spend a lot of time searching and try and error. Please help.

添加了Makefiles.am

Added the Makefiles.am

在父目录中, 我有两个文件Makefile.am和configure.ac:

in parent directory, I have two files Makefile.am and configure.ac:

Makefile.am

Makefile.am

AUTOMAKE_OPTIONS = foreign
SUBDIRS=src lib

configure.ac

configure.ac

AC_INIT([helloworld], [0.1], [helloworld@gmail.com])
AM_INIT_AUTOMAKE
AC_PROG_RANLIB
AC_LANG(C++)
AC_PROG_CC
AC_PROG_CXX
AC_CONFIG_MACRO_DIR([m4])
AC_PROG_LIBTOOL
AC_DISABLE_STATIC
AC_CONFIG_FILES([Makefile lib/Makefile src/Makefile])
AC_SUBST([CC])
LT_INIT
AC_OUTPUT

在lib目录中有一个Makefile.am

In lib directory has one Makefile.am

INCDIR=../include
INCPATH=-I. -I$(INCDIR)
AM_CPPFLAGS=$(INCPATH)
lib_LTLIBRARIES=libhelloworld.la
libhelloworld_la_SOURCES=helloworld.cpp

在src目录中有一个Makefile.am

In src directory has one Makefile.am

INCDIR=../include
INCPATH=-I. -I$(INCDIR)
AM_CPPFLAGS=$(INCPATH)
helloworld_LDADD=-L/lib/libhelloworld.la
bin_PROGRAMS=helloworld
helloworld_SOURCES=main.cpp

如果我拿出成功,则编译成功 a.getvalue();

Compiled success if I take out the a.getvalue();

推荐答案

如果坚持使用递归make,则需要向src/Makefile.am添加一个特殊规则,以确保用于重建库的lib/Makefile.am规则是已应用(此处未试用,但已在其他项目中使用).

If you insist on using recursive make, you will need to add a special rule to src/Makefile.am to make sure the lib/Makefile.am rules for rebuilding the library are applied (untested here, but used in other projects).

这意味着正常的递归构建将先构建.includelibsrc,然后再构建lib.我强烈建议您放弃递归构造,并使用我在其他答案中列出的一个Makefile.am解决方案.

This now means that a normal recursive build will build ., include, lib, src, and then lib again. I would strongly recommend ditching recursive make and use the one Makefile.am solution I have laid out in my other answer.

bin_PROGRAMS        = helloworld
helloworld_CPPFLAGS = $(top_srcdir)/include
helloworld_LDADD    = $(top_builddir)/lib/libhelloworld.la
helloworld_SOURCES  = main.cpp

$(top_builddir)/lib/libhelloworld.la:
        cd $(top_builddir)/lib && $(MAKE) libhelloworld.la

这篇关于C ++程序(.cpp)如何与标头(.h)和libtool(.la)一起使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆