包括一个lib头文件 [英] including a lib header file

查看:74
本文介绍了包括一个lib头文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的系统上的库中有一行像

#include< path / to / header.h是正确的

文件并使用这个库提供的一些函数在一个类的

实现文件(file.cpp)中,而不是声明那些

函数头文件中的类声明?


谢谢

解决方案

Gary Wessle写道:
< blockquote class =post_quotes>
我的系统上的库中有一行像

#include< path / to / header.h是正确的

文件并在类中使用此库提供的一些函数在类中的

实现文件(file.cpp)中,在类中声明那些

函数头文件中的声明?



如果它适合你,你的谜语是什么?


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问


Gary Wessle写道:




是否有权像

#include< path / to / header.h用于我系统上的库,在我的标题中

文件并使用该库提供的一些功能
一个类中的
实现文件(file.cpp),在头文件的类声明中声明了那些

函数?


谢谢



信不信由你,这可能是一个复杂的主题。


如果你需要库头中的东西,以便写下你自己的

标题,这是典型的:


mystuff.h:


#include&q uot; path / to / somelib.h"


a_type_from_somelib a_function_that_i_provide();


mystuff.cpp


#include" mystuff.h"

a_type_from_somelib a_function_that_i_provide()

{

返回a_function_from_somelib();

}


如果你不需要图书馆标题中的东西来写你自己的标题,但你做的是需要来自图书馆的东西以便

编写你的实现然后这是典型的:


mystuff.h:


void a_function_that_i_provide();


mystuff.cpp


#include" myclass.h"

#include" path / to / somelib.h"


void a_function_that_i_provide()

{

a_function_from_somelib(); < br $>
}


当然,如果你根本不需要图书馆标题,那么这是典型的




mystuff.h:


void a_function_that_i_provide();


mystuff.cpp


#包括myclass.h


无效a_function_that_i_provide()

{

}


第一种和第二种情况之间的区别在于,在第一种情况下,您将用户暴露给somelib.h,而在第二种情况下,您需要将b / b
不。温和地不喜欢暴露它们。不要强烈赞美它。例如,不要走这么远:


*********步伐太快********

mystuff.h:


// my_own_type与a_type_from_somelib相同

//我看了那个类型并且自己做了以避免

//包括somelib.h

typedef int my_own_type;

my_own_type a_function_that_i_provide();


mystuff .cpp


#include" path / to / somelib.h"

#include" mystuff.h"


my_own_type a_function_that_i_provide()

{

返回a_function_from_somelib();

}

*** ******快节俭********


如果你遵循这些规则,那么你就是在尊重非正式的规则

我们所有人(c ++程序员)或多或少都遵循:如果我需要一些

来自另一个标题来使用标题中的内容,那么将其包含在

me中。如果我不,那就不要了。


你应该注意这种方法存在缺陷。考虑

这种可能性。


somethingelse.h


typedef int a_type_from_somelib;

typedef bool stealth_type;


somelib.h

#include" path / to / somethingelse.h"


a_type_from_somelib a_function_from_somelib();


mystuff.h

#include" path / to / somelib.h"


a_type_from_somelib a_function_that_i_provide();

mystuff.cpp


#include" mystuff。 h"


a_type_from_somelib a_function_that_i_provide()

{

返回a_function_from_somelib();

}


void another_function()

{

stealth_type i_am_a_stealth_dependency;

}


mystuff.cpp中的代码对somethingelse.h具有隐秘依赖性。

stealth_type可以在mystuff.cpp中使用,因此程序

编译。但这是一个意外:stealth_type可用的原因是
是需要a_type_from_somelib。


维护somelib.h的人会很完美在他的(非正式)

权利中,决定他不再需要包含somethingelse.h和

这样改写:


somelib.h


typedef int a_type_from_somelib;

a_type_from_somelib a_function_from_somelib();


如果他这样做,mystuff .cpp因为隐身

依赖而不再编译。编写mystuff.cpp的正确方法是:


mystuff.cpp


#include" mystuff.h"

#include" path / to / somethingelse.h"

a_type_from_somelib a_function_that_i_provide()

{

返回a_function_from_somelib ();

}


void another_function()

{

stealth_type i_am_no_longer_stealthed;

}


如果你足够挑剔,那么你可以在你自己的代码中避免这个错误。


不幸的是,使用你的代码的人(包括mystuff.h)可能不那么苛刻,而且他们可能建立隐身依赖

somethingelse.h。当他们的代码爆炸时,当然,他们会责备你。


Howard Gardner< hg ****** @ rawbw.comwrites:
< blockquote class =post_quotes>
Gary Wessle写道:




是否有权像

#include< path / to / header.h为我系统上的库,在我的标题中

文件并使用这个库提供的一些函数在

实现文件(file.cpp)在一个类中,在头文件的类声明中声明那些

函数?

谢谢



信不信由你,这可能是一个复杂的主题。


如果你需要库头中的东西来写你的

自己的标题,这是典型的:


mystuff.h:


#include" path / to / somelib .h"


a_type_from_somelib a_function_that_i_provide();


mystuff.cpp


#include" mystuff.h"


a_type_from_somelib a_function_that_i_provide()

{

返回a_function_from_somelib();

}


如果你不需要库标题是为了写你自己的标题,但是你确实需要库中的东西来编写你的实现然后这是典型的:
< br $> b $ b mystuff.h:


无效a_function_that_i_provide();


mystuff.cpp

#include" myclass.h"

#include" path / to / somelib.h"


void a_function_that_i_provide()

{

a_function_from_somelib();

}


当然,如果你不''根本不需要图书馆标题,那么

是典型的:

mystuff.h:


无效a_function_that_i_provide();

mystuff.cpp


#include" myclass.h"


void a_function_that_i_provide()

{

}


第一种和第二种情况的区别在于

第一种情况是你将你的用户公开给somelib.h,而在第二种情况下你没有公开你的用户。温和地不喜欢暴露它们。不是很喜欢它,但是b $ b强烈。例如,不要走这么远:


*********步伐太快********

mystuff.h:


// my_own_type与a_type_from_somelib相同

//我看了那个类型并且自己做了以避免

//包括somelib.h

typedef int my_own_type;

my_own_type a_function_that_i_provide();


mystuff .cpp


#include" path / to / somelib.h"

#include" mystuff.h"


my_own_type a_function_that_i_provide()

{

返回a_function_from_somelib();

}

*** ******快节俭********


如果你遵守这些规则,那么你就是在履行非正式规则

我们所有人(c ++程序员)或多或少都遵循:如果我需要

来自另一个标题的东西来使用标题中的东西,那么

包括它给我。如果我不,那就不要了。


你应该注意这种方法存在缺陷。考虑

这种可能性。


somethingelse.h


typedef int a_type_from_somelib;

typedef bool stealth_type;


somelib.h

#include" path / to / somethingelse.h"


a_type_from_somelib a_function_from_somelib();


mystuff.h

#include" path / to / somelib.h"


a_type_from_somelib a_function_that_i_provide();

mystuff.cpp


#include" mystuff。 h"


a_type_from_somelib a_function_that_i_provide()

{

返回a_function_from_somelib();

}


void another_function()

{

stealth_type i_am_a_stealth_dependency;

}


mystuff.cpp中的代码对

somethingelse.h具有隐秘依赖性。你可以在mystuff.cpp中使用stealth_type,所以程序编译的是
。但这是一个意外:

stealth_type可用的原因是需要a_type_from_somelib。


维护somelib.h的人会很完美在他的

(非正式)权利中决定他不再需要包括

somethingelse.h并以这种方式重写:


somelib.h


typedef int a_type_from_somelib;

a_type_from_somelib a_function_from_somelib();


如果他这样做,mystuff .cpp因为隐身

依赖而不再编译。编写mystuff.cpp的正确方法是:


mystuff.cpp


#include" mystuff.h"

#include" path / to / somethingelse.h"

a_type_from_somelib a_function_that_i_provide()

{

返回a_function_from_somelib ();

}


void another_function()

{

stealth_type i_am_no_longer_stealthed;

}


如果你足够挑剔,那么你可以在你自己的代码中避免这个错误。


不幸的是,使用你的代码的人(包括mystuff.h)可能不那么苛刻,而且他们可能建立隐身依赖

somethingelse.h。当他们的代码爆炸时,当然,他们会责怪你b



谢谢


你没有明确提到我是否可以放置

a_function_from_somelib公开范围a / b
a_class_that_i_provide但我认为你暗示了它。若然,那么

为什么我收到这个看似链接器错误的错误?


************* ***开始出错****************

fred @ debian:〜/ myPrograms / common


Hi
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?

thanks

解决方案

Gary Wessle wrote:

is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?

If it works for you, what is your conccern?

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Gary Wessle wrote:

Hi
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?

thanks

Believe it or not, this can be a complicated subject.

If you need something from the library header in order to write your own
header, then this is typical:

mystuff.h:

#include "path/to/somelib.h"

a_type_from_somelib a_function_that_i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_somelib a_function_that_i_provide()
{
return a_function_from_somelib();
}

If you do NOT need something from the library header in order to write
your own header, but you do need something from the library in order to
write your implementations then this is typical:

mystuff.h:

void a_function_that_i_provide();

mystuff.cpp

#include "myclass.h"
#include "path/to/somelib.h"

void a_function_that_i_provide()
{
a_function_from_somelib();
}

And of course, if you don''t need the library header at all, then this is
typical:

mystuff.h:

void a_function_that_i_provide();

mystuff.cpp

#include "myclass.h"

void a_function_that_i_provide()
{
}

The difference between the first and the second case is that in the
first case you expose your users to somelib.h, while in the second case
you do not. Mildly prefer not to expose them. Don''t prefer it strongly
though. For example, don''t go this far:

********* A STEP TOO FAR ********
mystuff.h:

// my_own_type is the same as a_type_from_somelib
// I looked that type up and made my own just to avoid
// including somelib.h
typedef int my_own_type;
my_own_type a_function_that_i_provide();

mystuff.cpp

#include "path/to/somelib.h"
#include "mystuff.h"

my_own_type a_function_that_i_provide()
{
return a_function_from_somelib();
}
********* A STEP TOO FAR ********

If you follow those rules, then you are honoring the informal rule that
all of us (c++ programmers) more or less follow: If I need something
from another header to use the stuff in your header, then include it for
me. If I don''t, then don''t.

There''s a flaw in this approach that you should be aware of. Consider
this possibility.

somethingelse.h

typedef int a_type_from_somelib;
typedef bool stealth_type;

somelib.h

#include "path/to/somethingelse.h"

a_type_from_somelib a_function_from_somelib();

mystuff.h

#include "path/to/somelib.h"

a_type_from_somelib a_function_that_i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_somelib a_function_that_i_provide()
{
return a_function_from_somelib();
}

void another_function()
{
stealth_type i_am_a_stealth_dependency;
}

The code in mystuff.cpp has a stealth dependency on somethingelse.h.
stealth_type is available to you in mystuff.cpp, so the program
compiles. It''s an accident though: the REASON that stealth_type is
available is that a_type_from_somelib is needed.

The guy who maintaind somelib.h would be perfectly within his (informal)
rights to decide that he no longer needs to include somethingelse.h and
rewrite this way:

somelib.h

typedef int a_type_from_somelib;
a_type_from_somelib a_function_from_somelib();

If he did, mystuff.cpp wouldn''t compile anymore because of the stealth
dependency. The proper way to write mystuff.cpp is:

mystuff.cpp

#include "mystuff.h"
#include "path/to/somethingelse.h"

a_type_from_somelib a_function_that_i_provide()
{
return a_function_from_somelib();
}

void another_function()
{
stealth_type i_am_no_longer_stealthed;
}

If you are fastidious enough, then you can avoid this mistake in your
own code.

Unhappily, the people who use your code (include mystuff.h) may not be
so fastidious, and they might build in a stealth dependency on
somethingelse.h. When their code blows up, of course, they will blame you.


Howard Gardner <hg******@rawbw.comwrites:

Gary Wessle wrote:

Hi
is it right to have a line like
#include <path/to/header.hfor a library on my system, in my header
file and use some functions provided by this library in the
implementation file (file.cpp) inside a class with out declaring those
functions in the class declaration in the header file?
thanks


Believe it or not, this can be a complicated subject.

If you need something from the library header in order to write your
own header, then this is typical:

mystuff.h:

#include "path/to/somelib.h"

a_type_from_somelib a_function_that_i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_somelib a_function_that_i_provide()
{
return a_function_from_somelib();
}

If you do NOT need something from the library header in order to write
your own header, but you do need something from the library in order
to write your implementations then this is typical:

mystuff.h:

void a_function_that_i_provide();

mystuff.cpp

#include "myclass.h"
#include "path/to/somelib.h"

void a_function_that_i_provide()
{
a_function_from_somelib();
}

And of course, if you don''t need the library header at all, then this
is typical:

mystuff.h:

void a_function_that_i_provide();

mystuff.cpp

#include "myclass.h"

void a_function_that_i_provide()
{
}

The difference between the first and the second case is that in the
first case you expose your users to somelib.h, while in the second
case you do not. Mildly prefer not to expose them. Don''t prefer it
strongly though. For example, don''t go this far:

********* A STEP TOO FAR ********
mystuff.h:

// my_own_type is the same as a_type_from_somelib
// I looked that type up and made my own just to avoid
// including somelib.h
typedef int my_own_type;
my_own_type a_function_that_i_provide();

mystuff.cpp

#include "path/to/somelib.h"
#include "mystuff.h"

my_own_type a_function_that_i_provide()
{
return a_function_from_somelib();
}
********* A STEP TOO FAR ********

If you follow those rules, then you are honoring the informal rule
that all of us (c++ programmers) more or less follow: If I need
something from another header to use the stuff in your header, then
include it for me. If I don''t, then don''t.

There''s a flaw in this approach that you should be aware of. Consider
this possibility.

somethingelse.h

typedef int a_type_from_somelib;
typedef bool stealth_type;

somelib.h

#include "path/to/somethingelse.h"

a_type_from_somelib a_function_from_somelib();

mystuff.h

#include "path/to/somelib.h"

a_type_from_somelib a_function_that_i_provide();

mystuff.cpp

#include "mystuff.h"

a_type_from_somelib a_function_that_i_provide()
{
return a_function_from_somelib();
}

void another_function()
{
stealth_type i_am_a_stealth_dependency;
}

The code in mystuff.cpp has a stealth dependency on
somethingelse.h. stealth_type is available to you in mystuff.cpp, so
the program compiles. It''s an accident though: the REASON that
stealth_type is available is that a_type_from_somelib is needed.

The guy who maintaind somelib.h would be perfectly within his
(informal) rights to decide that he no longer needs to include
somethingelse.h and rewrite this way:

somelib.h

typedef int a_type_from_somelib;
a_type_from_somelib a_function_from_somelib();

If he did, mystuff.cpp wouldn''t compile anymore because of the stealth
dependency. The proper way to write mystuff.cpp is:

mystuff.cpp

#include "mystuff.h"
#include "path/to/somethingelse.h"

a_type_from_somelib a_function_that_i_provide()
{
return a_function_from_somelib();
}

void another_function()
{
stealth_type i_am_no_longer_stealthed;
}

If you are fastidious enough, then you can avoid this mistake in your
own code.

Unhappily, the people who use your code (include mystuff.h) may not be
so fastidious, and they might build in a stealth dependency on
somethingelse.h. When their code blows up, of course, they will blame
you.


thank you

you did not explicitly mention whether I can place
a_function_from_somelib in the public scope of a
a_class_that_i_provide but I take it that you implied it. if so, then
why I getting this error which appear to be a linker error?

**************** start error ****************
fred@debian:~/myPrograms/common


这篇关于包括一个lib头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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