473,591 Members | 2,871 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

how could i declare & define function in header file

i have just made a test project :(win32 console)

//file : func.h
#ifndef _FUNC_H_
#define _FUNC_H_

void func1()
{
return;
};

void func2()
{
return;
};

#endif

//file : use1.cpp
#include "func.h"

void user1()
{
func1();
func2();
}

//file : use2.cpp
#include "func.h"
void user1()
{
func1();
func2();
}

//file : main.cpp
int main(int argc, char* argv[])
{
return 0;
};

but it failed when linking
Linking...
use2.obj : error LNK2005: "void __cdecl func1(void)" (?func1@@YAXXZ)
already defined in use1.obj
use2.obj : error LNK2005: "void __cdecl func2(void)" (?func2@@YAXXZ)
already defined in use1.obj
Debug/HelloWorld.exe : fatal error LNK1169: one or more multiply
defined symbols found
Error executing link.exe.

but it works if the function is template function. why?

Jan 13 '06 #1
4 13216
Use the 'inline' keyword, that's what it's for.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 13 '06 #2
thanks , it works :)

Jan 13 '06 #3

Alf P. Steinbach wrote:
Use the 'inline' keyword, that's what it's for.


Well, the inline keyword wasn't made so you could define functions in a
header instead of a source file...they were made so that you could give
the compiler the hint to inline that function sort of like a safer
macro. You of course know this, but the OP might not. You don't
inline everything so that you can write the code in a header instead of
source file...you inline things that are short and you want to be
fast...no cost of function call. Normally you declare in a header and
define in source file. You don't do this with inline functions because
the compiler needs to know about the body in order to inline the code
and it needs to know this for each object file it compiles....the
linker is not involved.

Jan 13 '06 #4
* ro**********@gm ail.com:

Alf P. Steinbach wrote:
Use the 'inline' keyword, that's what it's for.


Well, the inline keyword wasn't made so you could define functions in a
header instead of a source file...they were made so that you could give
the compiler the hint to inline that function sort of like a safer
macro. You of course know this, but the OP might not. You don't
inline everything so that you can write the code in a header instead of
source file...you inline things that are short and you want to be
fast...no cost of function call. Normally you declare in a header and
define in source file. You don't do this with inline functions because
the compiler needs to know about the body in order to inline the code
and it needs to know this for each object file it compiles....the
linker is not involved.


I'm sorry, but that's incorrect in almost all respects except the
historical.

However, it's a very very common misconception, and so it's nothing to be
ashamed of not knowing.

First, the inline keyword does not, in practice, give any reliable
optimization hint: it is primarily in support of the one-definition rule,
and not for optimization.

Second, with a modern optimizing compiler there's no point in inlining
things that are short, because the compiler does that if you instruct it
optimize for speed and such inlining will increase speed (the compiler is
generally much better at determining that than the programmer).

Third, to do reliable machine code level inlining you'll have to use
compiler-specific means instead of the 'inline' keyword -- however, as
explained in the previous paragraph, that may not always be wise.

Fourth, with a modern compiler the linker is indeed involved in inlining
(look up "whole program optimization"), which then has nothing to do with
the keyword 'inline'.

Fifth, the compiler (+ linker) need not then have access to the textual
function definition to do machine code inlining.

And so on.

The upshot is to use 'inline' for the case of function definitions in header
files, and possibly also for self-documenting code (telling the _programmer_
that "I expect this to be inlined, use freely without thinking of cost") and
not for misguided optimization where it generally doesn't work, and where if
it works it may have the opposite effect of what you want, _preventing_ the
old compiler from doing a reasonable optimization.

Hope this helps,

- Alf

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jan 13 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2906
by: trying_to_learn | last post by:
im in the primary stages of learning C++. The book im learning from says Dont use using namespace.. directive in header file However im trying to make the following header file.I need to include <string> header file and also will have to say using namespace std to make the string class visible. qn)how do i resolve the requirement of *not* using the "using" directive in the header file and at the same time declare my structure in the...
1
11980
by: Travis Stewart | last post by:
Hi, I've been working on some software to control an older GPIB card for the past couple of weeks. I have function prototypes in a header file pc-mate2.h and the functions are defined in pc-mate2.c. I am using the Borland Turbo C 2.01 compiler (it is the compiler that the person I am writing this for uses) and when I compile the program I am writing I get an error: Turbo Link Version 2.0 Copyright (c) 1987, 1988 Borland...
3
2048
by: Sujan Datta | last post by:
What are the possible effects of modifying an existing header file, which includes bunch of defines, function prototypes and some struct definitions. The structure of the header file looks something like this //Start of header Define Define Define Define
6
1732
by: candy | last post by:
hi all, I just want to know that whether the C header files( like stdio.h,etc which the compiler provides) just contains the function declarations or they also contain some additionalinformation like where to look in the memory for the defintions of the standard functions like scanf(char*c,...)).
6
31482
by: Ravi | last post by:
Hi All: Is there any reason for declaring functions as static in a header file if that header file is going to be included in several other files? The compiler throws a warning for every such function declared but not called in the source file. Here is what I heard someone mention: The functions are declared static as an optimization. Making static "hidden-from-the-user" function to extern to appease -Wall compile argument is not a good...
0
3923
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen. It is almost like it is trying to implement it's own COM interfaces... below is the header, and a link to the dll+code: Zip file with header, example, and DLL:...
15
5315
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have private string myArray;
3
1794
by: thirunavukarasukm | last post by:
hai i want declare imports system.net -----header file in javascript <script> //// in this section how to declare imports system.net <script>
1
1822
by: AnagJohari | last post by:
please tell me how ro define a header file for a perticular function so i can use in another file please explain by giving example of any simple function suppose like addition of number just wanna know the basic concept. thank you
0
7934
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8236
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8362
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7992
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8225
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6639
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5732
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5400
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3891
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.