#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 Hiis it right to have a line like#include <path/to/header.hfor a library on my system, in my headerfile and use some functions provided by this library in theimplementation file (file.cpp) inside a class with out declaring thosefunctions in the class declaration in the header file?thanks 解决方案If it works for you, what is your conccern?V--Please remove capital ''A''s when replying by e-mailI do not respond to top-posted replies, please don''t askBelieve it or not, this can be a complicated subject.If you need something from the library header in order to write your ownheader, 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 writeyour own header, but you do need something from the library in order towrite 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 istypical: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 thefirst case you expose your users to somelib.h, while in the second caseyou do not. Mildly prefer not to expose them. Don''t prefer it stronglythough. 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.htypedef 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 thatall of us (c++ programmers) more or less follow: If I need somethingfrom another header to use the stuff in your header, then include it forme. If I don''t, then don''t.There''s a flaw in this approach that you should be aware of. Considerthis possibility.somethingelse.htypedef 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 programcompiles. It''s an accident though: the REASON that stealth_type isavailable 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 andrewrite this way:somelib.htypedef 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 stealthdependency. 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 yourown code.Unhappily, the people who use your code (include mystuff.h) may not beso fastidious, and they might build in a stealth dependency onsomethingelse.h. When their code blows up, of course, they will blame you.Believe it or not, this can be a complicated subject.If you need something from the library header in order to write yourown 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 writeyour own header, but you do need something from the library in orderto 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 thisis 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 thefirst case you expose your users to somelib.h, while in the secondcase you do not. Mildly prefer not to expose them. Don''t prefer itstrongly 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 rulethat all of us (c++ programmers) more or less follow: If I needsomething from another header to use the stuff in your header, theninclude it for me. If I don''t, then don''t.There''s a flaw in this approach that you should be aware of. Considerthis 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 onsomethingelse.h. stealth_type is available to you in mystuff.cpp, sothe program compiles. It''s an accident though: the REASON thatstealth_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 includesomethingelse.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 stealthdependency. 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 yourown code.Unhappily, the people who use your code (include mystuff.h) may not beso fastidious, and they might build in a stealth dependency onsomethingelse.h. When their code blows up, of course, they will blameyou.thank youyou did not explicitly mention whether I can placea_function_from_somelib in the public scope of aa_class_that_i_provide but I take it that you implied it. if so, thenwhy I getting this error which appear to be a linker error?**************** start error ****************fred@debian:~/myPrograms/common 这篇关于包括一个lib头文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-03 05:24