473,796 Members | 2,903 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

linking C++ functions in a C program

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-lit...c-and-cpp.html

Unfortunately it does not work.
Here is what I'm doing:

-----------------------------------------
//main.c

#include "mylib.h"

int main (int argc, char *argv[]){
foo();
return 0;
}
-----------------------------------------
//mylib.h

#indef __cplusplus
extern "C"{
#endif

void foo();

#ifdef __cplusplus
}
#endif
-----------------------------------------
//mylib.cc

#include <iostream>
#include "mylib.h"

using namespace std; // don't know what for!

void foo(){
cout<<"Hello World!\n";
}
-----------------------------------------
Thanks a lot for any suggestion.

Al
Nov 6 '08 #1
38 2668
abasili wrote:
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-lit...c-and-cpp.html

Unfortunately it does not work.
It does if you follow all the points. The first two are often overlooked.
Here is what I'm doing:

-----------------------------------------
//main.c

#include "mylib.h"

int main (int argc, char *argv[]){
foo();
return 0;
}
How are you building your executable?

--
Ian Collins
Nov 6 '08 #2
Ian Collins wrote:
>
It does if you follow all the points. The first two are often overlooked.
Here is my Makefile
>
How are you building your executable?

Here is my Makefile
-----------------------------------------

OS = OS_LINUX
CC = gcc
CXX = g++
AR = ar

INCMYLIB = mylib.h

INC = $(INCMYLIB)

OBJMYLIB = mylib.o

OBJ = $(OBJMYLIB)

ROOTINCL = $(ROOTSYS)/include

MYLIB = mylib.a

CFLAGS = -g -D$(OS) -Wall -O2 -fno-strict-aliasing \
-I/usr/X11R6/include/ -I/usr/X11R6/include/X11
-I$(ROOTINCL) \

BOOLFLAG = -D_BOOL
ROOTFLAG = `root-config --libs`
CCFLAG = $(BOOLFLAG) $(ROOTFLAG)

LDFLAGS = -L/usr/X11R6/lib -z muldefs
LOADMYLIBES = $(MYLIB) -lm -lX11 -Xlinker -Bstatic -lforms -Xlinker
-Bdynamic

INT = main

EXT =

EXE = $(EXT) $(INT)
#------------------------------------------------------------------------------

all: $(OBJ) $(MYLIB) $(EXE)

#------------------------------------------------------------------------------

%.o: %.c $(INC)
$(CC) -c -o $*.o $*.c $(CFLAGS)

%.o: %.cc $(INC)
$(CXX) -c -o $*.o $*.cc $(CFLAGS) $(BOOLFLAG)

$(MYLIB): $(OBJ)
$(AR) cru $(MYLIB) $(OBJ)

%: %.c $(MYLIB)
$(CC) -o $* $*.c $(CFLAGS) $(LDFLAGS) $(LOADMYLIBES)

%: %.cc $(MYLIB)
$(CXX) -o $* $*.cc $(CFLAGS) $(CCFLAG) $(LDFLAGS) $(LOADMYLIBES)

#------------------------------------------------------------------------------
export:
cp -p -f $(EXT) /usr/local/bin

#------------------------------------------------------------------------------

clean:
rm -f *~ *.bak *.o $(EXE) core

#============== =============== =============== =============== =============== ====
This Makefile is used to compile several programs and be able to use
somebody else library, here I reduced it to the minimum only to study
this case.
Thanks,

Al
Nov 6 '08 #3
Ian Collins wrote:
It does if you follow all the points. The first two are often overlooked.
I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++, there
are too many incompatibiliti es and a lot of work has been already done in C.
So I have to find another way to call a C++ function through my C code.
Nov 6 '08 #4
abasili wrote:
So I have to find another way to call a C++ function through my C code.
Problem is solved.
I created a new main.cc which will call my main_C.c which will be
compiled with gcc, while the main.cc is compiled with g++.
It's rather tricky but it works.
If anyone else have a different idea i'll be happy to change my approach.
Cheers,

Al
Nov 6 '08 #5
abasili wrote:
Ian Collins wrote:
>>
It does if you follow all the points. The first two are often
overlooked.

Here is my Makefile
>>
How are you building your executable?


Here is my Makefile
As Ian said, the first two points are often overlooked, and your
makefile has violated both of them. You compile main() with C instead of
C++, and you use the C compiler rather than the C++ compiler to link the
modules together.

Note: it would have been better to post this message to comp.lang.c++.
The ability to link C code with C++ is defined as part of the C++
standard. The C standard says nothing about that capability. You'll find
more people in that group who can help you than you can find in this group.
Nov 6 '08 #6
abasili wrote:
....
#indef __cplusplus
That should be #ifdef
//mylib.cc

#include <iostream>
#include "mylib.h"

using namespace std; // don't know what for!
For cout, of course. Without that using statement, you'd have to refer
to std::cout.
void foo(){
cout<<"Hello World!\n";
}
Nov 6 '08 #7
abasili wrote:
Ian Collins wrote:
>It does if you follow all the points. The first two are often
overlooked.

I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++,
there are too many incompatibiliti es and a lot of work has been
already done in C. So I have to find another way to call a C++
function through my C code.
In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons. C
function names are not. So the C system can't link the C++ code.

--
[mail]: Chuck F (cbfalconer at maineline dot net)
[page]: <http://cbfalconer.home .att.net>
Try the download section.
Nov 6 '08 #8


CBFalconer wrote:
abasili wrote:
Ian Collins wrote:
It does if you follow all the points. The first two are often
overlooked.
I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++,
there are too many incompatibiliti es and a lot of work has been
already done in C. So I have to find another way to call a C++
function through my C code.

In general, you just can't do that. C++ function names are
normally altered by the C++ compiler for various reasons.
True, but extern "C" turns that off. The only part of his program
compiled with the C++ compiler is a function declared with extern "C".
C
function names are not. So the C system can't link the C++ code.
That may be true; but not for the reason given.
Nov 6 '08 #9
abasili wrote:
Ian Collins wrote:
>It does if you follow all the points. The first two are often
overlooked.
I'm sorry about that, actually compiling with g++ the main works.
The main problem is that my C program cannot be compiled with g++, there
are too many incompatibiliti es and a lot of work has been already done
in C.
So I have to find another way to call a C++ function through my C code.
Always compile C++ source files with a C++ compiler.
Always compile C source files with a C compiler.
If you have a mix of object files from the two languages, use the C++
compiler's linker.

Problem solved.

S
Nov 6 '08 #10

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

Similar topics

1
2591
by: Jeff Hagelberg | last post by:
I'm trying to create a python module which can be used by a python interpreter embedded inside a fortran program I have. To do this, I first created python wrappers for all the functions in my fortran program using f2py. I then start an embedded python interpreter in c code which I link against the fortran program. I invoke the fortran program with a filename containing python code. This file is passed to the c code which passes it on...
7
5125
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
3241
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...
1
6098
by: Venky | last post by:
I'm compiling a C program that is using Interbase 6.0 APIS. Getting the following errors at the time of linking. Linking test.exe: Linker Warning: No module definition file specified: using defaults Linker Error: Undefined symbol isc_detach_database in module TEST.C ..... ..... If I set the option not to generate the underscore (function prefixed with "_") I get the following errors. Interbase 6.0 APIs resolved
1
313
by: Peetah_junkmail | last post by:
Hi, I'm not sure this is completely a C related question since it's more about linking problems, so don't hesitate to redirect me to a more appropriate NG. I have a set of useful functions (let's call this SUF) that facilitates my life when coding, such as memory allocation wrappers or generic string functions. I have a project composed of a shared library (SL) and a main program (MP) using SL. For some reasons, I don't want to make a...
3
3527
by: Kevin Burton | last post by:
I am trying to use managed C++ but I am getting the following link errors: Metadata file 'D:\Projects\Visa\AddressVerification\AddressVerificat ionTest\bin\Debug\AddressVerificationInterface.dll' could not be found AddressVerificationInterface error LNK2001: unresolved external symbol "void * __cdecl operator new(unsigned int)" (??2@$$FYAPAXI@Z)
3
4375
by: walkeraj | last post by:
I'm trying to compile an open source game called IVAN , and I'm able to compile it from a makefile, but not from an IDE. I have attempted to recreate the way the makefile compiles the project as closely as possible, yet I'm getting odd linking errors. I think I've traced it to some kind of problem with the SDL libraries it uses, yet I can't explain why it will compile from the makefile and not from the IDE. I have made sure that the ide...
6
2493
by: Joe.pHsiao | last post by:
Hi, I tried to link a C program to a library which is written by me in C+ +. I read some posts about linking a C program to C++ libraries. It seems doable by adding extern "C" to the C++ head file and to the function body modifier. However, in my test, it still doesn't work. The linking error messages are like undefined reference for new operator, and undefined reference from dequeue.tcc. ( sorry I don't know how to copy lines from...
10
2200
kiseitai2
by: kiseitai2 | last post by:
Hi everyone. My problem is tha Dev-C++ compiles perfectly fine but it fails in the linking process. I receive the linker error "multiple definitions of"; howerver, I modified the names of the functions in three of the files, checked my code to make sure I wasn't redefining my functions, changed code back and forth from .cpp to .h, and even excluded some files from the project and included them manually. Also, I even recopied the lines of code...
0
9527
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
10453
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
10223
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
10003
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
9050
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...
0
5573
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4115
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2924
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.