473,398 Members | 2,368 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Multiple functions (one version being inline and other beingnon-inline)

Hi Everyone,

I have the following code,

file1.cpp
---------

#include <cstdio>

inline int sample1()
{
printf("1::sample1\n");
return(0);
}

int main()
{
sample1();
return(0);
}
file2.cpp
---------

#include <cstdio>

int sample1()
{
printf("2::sample1\n");
return(0);
}
when i build both the files and exeucte, i get the following output,

2::sample1
I expected a linker error, as file2.o is exporting sample1 which is
already available in file1.o... What does the standard indicate for
such scenarios?

Thanks in advance !!!
Feb 27 '08 #1
3 1302
Rahul schrieb:
I expected a linker error, as file2.o is exporting sample1 which is
already available in file1.o... What does the standard indicate for
such scenarios?
You must not define the same object twice in a different way. Otherwise
- as you might guess - undefined behaviour. (Look for the ODR.)
Marcel
Feb 27 '08 #2
On Feb 27, 11:28*pm, Marcel Müller <news.5.ma...@spamgourmet.com>
wrote:
Rahul schrieb:
I expected a linker error, as file2.o is exporting sample1 which is
already available in file1.o... What does the standard indicate for
such scenarios?
I dont have knowledge of what standard says but I tried this with g++
2.95.3 and g++ is doing is
1] Keeps the sample1 funcion in file1.cpp as weak symbole in the
object file ( Probably because you have made it inline )
2] sample1 funcion in the file2.cpp is treated as Global symbol.

So when you link this two object modules the Global symbole gets
priority over Weak Symbol and you dont get any linker errors.

If you remove inline in sample1 funcion form file1.cpp, you should get
linker error.

Feb 28 '08 #3
On Feb 27, 11:59 am, Rahul <sam_...@yahoo.co.inwrote:
I have the following code,
file1.cpp
---------
#include <cstdio>

inline int sample1()
{
printf("1::sample1\n");
return(0);
}
int main()
{
sample1();
return(0);
}
file2.cpp
---------
#include <cstdio>
int sample1()
{
printf("2::sample1\n");
return(0);
}
when i build both the files and exeucte, i get the following output,
2::sample1
I expected a linker error, as file2.o is exporting sample1
which is already available in file1.o... What does the
standard indicate for such scenarios?
It's undefined behavior, so anything the compiler does with it
is correct. §7.1.2/4: "If a function with external linkage is
declared inline in one translation unit, it shall be declared
inline in all translation units in which it appears; no
diagnostic is required."

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Feb 28 '08 #4

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

Similar topics

5
by: Mike | last post by:
I have some code I am working on. One function reads a file using fso, then I have multiple functions using the string read from the file. Each function splits the lines up and will return one...
6
by: Atchoum | last post by:
Hi, I have a solution with multiple projects (one C# and the others VB) and right they all compile to their own DLL. Is there not a way to get a unique DLL as output? TIA, Atchoum
2
by: Wei Wang | last post by:
Hi, Can I call multiple functions in one trigger? Something like this: CREATE TRIGGER match_cond_name_generate BEFORE INSERT OR UPDATE ON public.predicate_index FOR EACH ROW EXECUTE...
6
by: Carlos J. Quintero [VB MVP] | last post by:
Hi, This is a question that is asked quite often, so here is some recent info from insde Microsoft: Why Visual Studio targets only one version of the .NET Framework...
2
by: jasonsgeiger | last post by:
From: "Factor" <jasonsgeiger@gmail.com> Newsgroups: microsoft.public.in.csharp Subject: Multiple Clients, One port Date: Wed, 19 Apr 2006 09:36:02 -0700 I'm been working with sockets for a...
13
LacrosseB0ss
by: LacrosseB0ss | last post by:
I was wondering, can an OnClick event of a button call multiple functions? The reason for this is I have a page that when "ok" is clicked, goes to a preview page before submitting data to the...
7
by: pronerd | last post by:
Does any one know how to add a function to an event handler with out losing the current event handler? For example you can add multiple functions to a single event handler with something like : ...
3
by: Harshpandya | last post by:
<form action="?= $postUri ?>" method="post" onSubmit="return verifyInput();" onReset="setTimeout('toggleDHCP()',100) setFormDisabled(true);"> In this code - i want to pass multiple functions...
1
by: su817200 | last post by:
Dear Friend, I would like to run multiple functions on Button Click eg... <input type="button" value="Update" name="B1" onclick="ReadFileToString() timedRefresh(1500)"></p> It is giving...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
0
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...
0
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,...
0
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...

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.