Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Including Global Functions

Posted on 2011-03-21
9
Medium Priority
?
322 Views
Last Modified: 2012-05-11
What is the most efficient way to load a global function one time despite being used in many (.CPP) classes? My application launches a function in a background thread (AfxBeginThread) from many classes in the application.

When I link the application DLL, I get the message:


1>clsGlobals.obj : warning LNK4006: "unsigned int __cdecl MessageThread(void *)" (?MessageThread@@YAIPAX@Z) already defined in helper.obj; second definition ignored

I tried to bury the global function in a .H file. Code snippet below.

#pragma once
#ifndef GLOBALDEFINITIONS_H
#define GLOBALDEFINITIONS_H


#include "Helper.h"
#include "cMsgPacket.h"

 class clsGlobals
{
public:
	clsGlobals(void);
	virtual ~clsGlobals(void);
	
};

UINT MessageThread(LPVOID pParam)
{
	if (pParam)
	{
		CString* lpszMsg = (CString*)pParam;
		AfxMessageBox(*lpszMsg);
		delete lpszMsg;
	}
	return 0;
}
#endif

Open in new window

0
Comment
Question by:TimPeer
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
9 Comments
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35183472
I think your snippet has some lines missing - it should end with a
#endif


Are you certain that MessageThread isn't defined somewhere else?
0
 
LVL 86

Accepted Solution

by:
jkr earned 2000 total points
ID: 35183722
First of all, you should not do that - it is always better to add these in a separate .cpp file. Header guards (http://faculty.cs.niu.edu/~mcmahon/CS241/c241man/node90.html) as you are using them will not prevent you from linker errors, to avoid that one, try the following:
#infdef MessageThread_IMPLEMENTED
UINT MessageThread(LPVOID pParam)
{
	if (pParam)
	{
		CString* lpszMsg = (CString*)pParam;
		AfxMessageBox(*lpszMsg);
		delete lpszMsg;
	}
	return 0;
}
#define MessageThread_IMPLEMENTED
#else
UINT MessageThread(LPVOID pParam); // just a declaration in this case
#endif

Open in new window

0
 

Author Comment

by:TimPeer
ID: 35184666
I think I implemented the CPP and H as you described. Here is an example of another... not working.

Here is the message:
1>c:\envy\dev\replicator console\nvlibrary\_nfxthrewdbexception.h(6): error C2144: syntax error : 'void' should be preceded by ';'


Header:
#pragma once
#include "_NDatabase2.h"
#include <afxver_.h>


void AFXAPI NfxThrowDBException(RETCODE nRetCode, _NDatabase* pdb, HSTMT hstmt);

Open in new window


CPP implementation
#include "stdafx.h"
#include "_NFXThrewDBException.h"
#include "_NDatabase2.h"
#include "Helper.h"

#ifndef NfxThrowDBException_IMPLEMENTED

void AFXAPI NfxThrowDBException(RETCODE nRetCode, _NDatabase* pdb, HSTMT hstmt)
{
	_NDBException* pException = new _NDBException();
	if (nRetCode == SQL_ERROR && pdb != NULL)
		pException->BuildErrorString(pdb, hstmt);
	else if (nRetCode > AFX_SQL_ERROR && nRetCode < AFX_SQL_ERROR_MAX)
	{
		VERIFY(pException->m_strError.LoadString(
			AFX_IDP_SQL_FIRST+(nRetCode-AFX_SQL_ERROR)));
		TRACE1("%s\n", pException->m_strError);
	}
	THROW(pException);
}
#define NfxThrowDBException_IMPLEMENTED
#else 

	void AFXAPI NfxThrowDBException(RETCODE nRetCode, _NDatabase* pdb, HSTMT hstmt);

#endif

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 86

Expert Comment

by:jkr
ID: 35184703
Well, in that case I'd check if the file that includes that header file isn't responsible for the missing ';' - the header itself looks healthy.
0
 

Author Comment

by:TimPeer
ID: 35185406
Thank you... It seems your correction worked. The ";" message was caused by the previous header as examined in the compilation log.

A followup please... '#pragma once'. What is the purpose of this precompiler directive?

Thanks again.
0
 

Author Comment

by:TimPeer
ID: 35185407
Thank you... It seems your correction worked. The ";" message was caused by the previous header as examined in the compilation log.

A followup please... '#pragma once'. What is the purpose of this precompiler directive?

Thanks again.
0
 
LVL 86

Expert Comment

by:jkr
ID: 35185455
'#pragma once' has a similar effect as header guards, yet it is specific to VC++ - see http://msdn.microsoft.com/en-us/library/4141z1cx.aspx ("once"): "Specifies that the file will be included (opened) only once by the compiler when compiling a source code file."
0
 

Author Comment

by:TimPeer
ID: 35185475
Sorry wrong solution.
0
 

Author Closing Comment

by:TimPeer
ID: 35185476
A Correction to your solution. Many thanks for your expert comments on the use of Headers.

#ifndef MessageThread_IMPLEMENTED
UINT MessageThread(LPVOID pParam)
{
      if (pParam)
      {
            CString* lpszMsg = (CString*)pParam;
            AfxMessageBox(*lpszMsg);
            delete lpszMsg;
      }
      return 0;
}
#define MessageThread_IMPLEMENTED
#else
UINT MessageThread(LPVOID pParam); // just a declaration in this case
#endif
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question