473,614 Members | 2,508 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Linking problem while calling C function in CPP

4 New Member
hi, my problem is linking my cli module with XORP modules.

I am calling my cli_main() in the XORP router manager module rtrmgr.
It takes the files and compiles the files that are in the router manager
but it gives undefined references to some objects or methods which r in the other modules like libxorp libxipc ... etc.

I wrote Makefiles for each module when I run global cli Makefile rtrmgr Makefile didn't link with the libxorp module.
Nov 15 '07 #1
3 2681
weaknessforcats
9,208 Recognized Expert Moderator Expert
You cannot call C functions in C++ unless C++ has been told that they are C functions. Otherwise, the function name gets altered (mangled) into a has name. The C++ program uses the hash name so when you call using the C name a match is never found and ytou have an unresolved external reference.

All C functions to be called in C++ must be qualified as extern "C" in C++.
Nov 15 '07 #2
kalyani1031
4 New Member
hi,I already included extern "C" eventhough I got some undefined references while
I am calling the method which is in one module and the definition of that method is in the other module. It gives some linking problem like

: undefined reference to `EventLoop::Eve ntLoop()'
245 ./rtrmgr/librtrmgr.a(mai n_rtrmgr1.o)(.t ext+0x4c9):src/main_rtrmgr1.cc :198: undefined refe rence to `EventLoop::~Ev entLoop()'
Nov 16 '07 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
You cannot call class member functions from C.

That's because the first argument of the member function is a hidden argument that is the object's this pointer. This argument cannot be specified by you. Therefore, you can't call the member function unbkles you are in C++.

You will need to write a wrapper on the C++ side. Call the wrapper from C, the wrapper calls the member function. The object's this pointer would be passed as a void*.

Assume your C++ class is EventLoop and you want to create an EventLoop object by calling EventLoop::Even tLoop() from C. Here is the workaround:
Expand|Select|Wrap|Line Numbers
  1. /* on the C side: */
  2. void* EventLoop;   /*the EventLoop object
  3. EventLoop = CreateEventLoop();
  4.  
  5. /* on the C++ side */
  6. void* CreateEventLoop()
  7. {
  8.      return (void*) new EventLoop;
  9. }
  10.  
Now to call int EventLoop::Meth odA(int) you would write a wrapper:
Expand|Select|Wrap|Line Numbers
  1. /* on the C++ side: */
  2. extern "C"
  3. int EventLoopMethodA(void* obj, int arg)
  4. {
  5.      EventLoop* temp = (EventLoop*)obj;
  6.     return obj->MethodA(arg);
  7. }
  8.  
  9. /* on the C side: */
  10. void* EventLoop;   /*the EventLoop object
  11. EventLoop = CreateEventLoop();
  12. int result = EventLoopMethodA(EventLoop, 3);
  13.  
Nov 16 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

7
5101
by: Steven T. Hatton | last post by:
Is there anything that gives a good description of how source code is converted into a translation unit, then object code, and then linked. I'm particularly interested in understanding why putting normal functions in header files results in multiple definition errors even when include guards are used. -- STH Hatton's Law: "There is only One inviolable Law" KDevelop: http://www.kdevelop.org SuSE: http://www.suse.com Mozilla:...
20
3218
by: Steven T. Hatton | last post by:
I just read this in the description of how C++ is supposed to be implemented: "All external object and function references are resolved. Library components are linked to satisfy external references to functions and objects not defined in the current translation. All such translator output is collected into a program image which contains information needed for execution in its execution environment." What I'm wondering is what exactly...
2
7244
by: | last post by:
Help! I'm new to c++, and am breaking my teeth on MS Visual C++ (bundled within Visual Studio .NET 2003). Am trying to link simple c++ code to fortran dlls created in Compaq Visual Fortran (v6.1). Posts concerning this topic are common, but none of the posted solutions I've tried work correctly with the above software. The linker can't seem to find the dll (reports 'unresolved external symbol __imp__IMSL_FUN@8'; IMSL_FUN.dll is the f77...
7
6545
by: wmkew | last post by:
Hello everyone I'm encountering a R6002 Runtime error and several bugs when trying to generate a simple Managed C++ application with .NET 2003. The main problem seems to arise from linking with LIBCMT(D).DLL. (My requirement is that we can't link with MSVCRT(D).LIB.) Below are steps I've followed, and the resulting problems 1. Using the New Project wizard, generate a Visual C++ .NET Class Library project (call it "Doomed") and a VC++...
6
3443
by: Uli | last post by:
Hello, I'm trying to use a DLL (by static linking) which was compiled with Borland C++Builder (BCB) in Visual C++ (Visual-Studio 2003). All functions are declared with the directive 'extern "C"' so that a cross-compiler-usage must be possible. I created an import library (.lib) for MSVC by using the LIB function and using a .DEF file (as described in KB131313 -
0
2955
by: dotyet | last post by:
Hi Everyone, I am trying to build a DB2 UDB UDF which can perform regex over the table data. The user defined function will call an external .dll file to do the task. I am referring to the following link for doing so: http://www.ibm.com/developerworks/db2/library/techarticle/0301stolze/0301stolze.html While linking the below mentioned code, I am facing some errors. I have installed PCRE and am using Visual C++ 6.0 in 32-bit mode. The
2
1510
by: kalyani1031 | last post by:
hi, Iam calling CPP function in C , I get the following undefined references. ./mycppinc/libmycppinc.a(cpp_file.o)(.text+0xd): In function `std::__verify_grouping(char const*, unsigned int, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': : undefined reference to `std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const' ./mycppinc/libmycppinc.a(cpp_file.o)(.text+0x60): In...
2
1106
by: mdubey82 | last post by:
Dear Friends I am new to vc++; While debugging my vc++ project which is calling c# method (by referencing c# project) I got the linking error I just cant understand how to get rid of it .. Pls help me out to solve this problem. The error is some thing like. Error 88 error LNK2019: unresolved external symbol "extern "C" int __cdecl jpeg8_read_header(struct Dicom::Jpeg::jpeg_decompress_struct *,int)"...
38
2644
by: abasili | last post by:
Hi everyone, I'm trying to compile a C++ function and then call it from a C program. Since Google is my friend I've ended up to this link which seems very clear: http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html Unfortunately it does not work. Here is what I'm doing: -----------------------------------------
0
8198
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
8142
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8642
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
8591
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...
0
8444
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...
1
6093
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
5549
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
4058
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
1438
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.