I'm writing my thesis and I have problem with using C++ function in C code. I searched solution and I found a lot of them, but it didn't work anyway. Please explain it to me one more time.
To be quick I have something like below and after gcc main.c -o main I get undefined reference to 'cppfun'
cpp.h:
#pragma once
#ifdef __cplusplus
extern "C" {
#endi
void cppfun();
#ifdef __cplusplus
}
#endif
cpp.cpp:
#include <stdio.h>
#include "cpp.h"
void cppfun()
{
printf("cpp_fun");
}
main.c:
#include <stdio.h>
#indlude "cpp.h"
int main(int argc, char *argv[])
{
cppfun();
return 0;
}
When you combine C and C++, you should compile the translation unit containing the main function, as C++. Not opposite. This is a FAQ.
An undefined reference is usually because you haven't linked in the translation unit where the missing thing is. Your stated build command is
gcc main.c -o main
while it should be e.g.
gcc -c main.c
g++ -c cpp.cpp
g++ cpp.o main.o -o main
except as mentioned, the main translation unit should be in C++.
You need to include all .cpp files after main. Something like that:
g++ main.cpp other.cpp etc.cpp -o main
Firstly you compile your cpp code without link via -c compiler switch
g++ cpp.cpp -c
and you have cpp.o file then compile and link your main.c with cpp.h and cpp.o files via gcc
gcc main.c -o main cpp.o
I tested this answer via my linux server. It is work.
Related
I have main.cpp, MyClass.cpp and MyClass.h files.
main.cpp
#include <iostream>
#include "MyClass.h"
int main(){
MyClass foo(123);
std::cout << foo.getNumber();
}
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
class MyClass
{
public:
MyClass(int n);
int getNumber();
private:
int fav_number;
};
#endif // MYCLASS_H
MyClass.cpp
#include "MyClass.h"
MyClass::MyClass(int n) {
fav_number = n;
}
MyClass::getNumber(){
return fav_number;
}
Now this program compiles and works fine as a project in CodeBlocks, but if I try to compile main seperately (not in a project) I get undefined reference to MyClass::MyClass(int). I think it is because in MyClass.h there is no body for the functions as they're in the cpp file.
So my question is: how does this program compile as a project even though MyClass.cpp isn't included anywhere in the main or the header?
When you add your cpp file to IDE it adds it to compile sources.
Depends on your IDE, for example XCode has section compile sources:
If you compile in console with g++, you need to type in console:
g++ main.cpp MyClass.cpp
This means which source files to compile, after that linker should link their main.o MyClass.o files. IDE just do all this stuff by himself.
You compile each .cpp file independently (indeed, if you look at your compiler output, you should see main.o and MyClass.o). The header files simply tell the code that the definitions exist somewhere and after compiling, a linker is used to "link" the two .o files together. It is during the linking stage that the definitions are resolved, so when main.o refers to code in MyClass.o, the linker is what puts these together.
CodeBlocks hides this from you, but its calling out to your compiler and linker to do this. (Actually, if you call gcc with all of your .cpp files, it will call the linker for you too, but this is simply a convenience and you can do it in multiple steps too. If you are using gcc to compile, your linker is usually ld)
You tell it to.
When using an IDE, the list of files in the "project" determines what list of filenames the IDE sends to the compiler, in a build command.
When invoking the build command manually, you have to do that yourself.
For example:
g++ -o myprogram main.cpp MyClass.cpp
Or:
g++ -c main.cpp
g++ -c MyClass.cpp
g++ -o myprogram main.o MyClass.o
Obviously add other flags as needed (include paths etc).
On MyClass.cpp you've forgotten return type specified on its header MyClass.h as follows
int MyClass::getNumber(){
return fav_number;
}
From a terminal console you must pass to the compiler g++ as argument all the files *.cpp which main depends on otherwise you will have an error as follows:
$ g++ main.cpp
/tmp/ccDTbMs5.o: In function `main':
main.cpp:(.text+0x15): undefined reference to `MyClass::MyClass(int)'
main.cpp:(.text+0x21): undefined reference to `MyClass::getNumber()'
collect2: error: ld returned 1 exit status
As you can see the compiler is trying to look for object code that is not found. When you compile main.cpp along with all the cpp files corresponding to the includes on your main. In this manner it will work for you as follows:
$ g++ main.cpp MyClass.cpp -o myprogram
$
$ ./myprogram
123
Option -o change the output program to "myprogram".
More complex situations can be treat using builders such as "Makefile".
In case you do not have the implementation files .cpp for each of your header files .h , you should have instead the binary object code. This should be passed to the g++ accordanly.
$ g++ -c MyClass.cpp
$ g++ -c main.cpp
The previous lines creates MyClass.o and main.o. Notice that the -c is passed to the compiler to indicate that only compilation will be done. Now you can pass the objects to the compiler as follows linking all of then together:
$ g++ -o prog MyClass.o main.o
$ ./prog
123
This is my first go at making my own header file. I am trying to make a simple Hello World program in C++ on Ubuntu. made 3 files as follows :
//hello.h file
#ifndef HELLO_H
#define HELLO_H
//my own code
void hello();
#endif
//hello.cpp file
#include <iostream>
#include "hello.h"
using namespace std;
void hello()
{
cout << "This line is printed from header.";
}
//main.cpp file
#include <iostream>
#include "hello.h"
using namespace std;
int main()
{
cout << "in main" << endl << endl;
hello();
return 0;
}
I've tried
g++ -I /home/Desktop/hello/ -c hello.cpp -o hello.o
to compile header file and this command worked.
then, while doing
g++ -o main main.cpp
I am ending up with following error:
/tmp/ccb0DwHP.o: In function `main':
main.cpp:(.text+0x2e): undefined reference to `hello()'
collect2: error: ld returned 1 exit status
Please suggest whether changes need to be made in any file or in any command in the terminal?
thank you
You don't link to hello.o in the command below:
g++ -o main main.cpp
Try this:
g++ -o main main.cpp hello.o
Or for such simple program, just issue the command below:
g++ -o main main.cpp hello.cpp
For ease of use, create a makefile, then you just run make:
make
A simple makefile:
helloprogram: hello.h hello.cpp main.cpp
g++ -o helloprogram main.cpp hello.cpp
clean:
rm helloprogram
Put hello.h in Path2Hello;
g++ -o main -I Path2Hello main.cpp hello.cpp
ps: -I option to specify an alternate include directory (for header files).
To compile and run a C language program, you need a C compiler. To setup a C language compiler in your Computer/laptop, there are two ways:
Download a full fledged IDE like Turbo C or Microsoft Visual C++, which comes along with a C language compiler.
Or, you use any text editor to edit the program files and download the C compiler separately.
I have some problems to build a program using g++. The program is using a library that I have written in C called libiec60063. I want to write my new project in C++ (even if not yet familiar with C++) but I can't manage to link it correctly.
For example I have the following code in a file called main.cpp
#include <libiec60063.h>
int main() {
Select_IEC60063_Number(125, 12);
return 0;
}
I can compile the source correctly typing
g++ -I/home/workspace/a_CommonBinary/include -c main.cpp
If I want to link it i get some error message
g++ -L/home/workspace/a_CommonBinary_draft/lib -o main main.o -lm -liec60063
main.o: In function `main':
main.cpp:(.text+0x1b): undefined reference to `Select_IEC60063_Number(double, int)'
collect2: error: ld returned 1 exit status
If I rename the main-file to main.c I can compile and link the program correctly with the GCC-Compiler using the same parameters.
Can anybody explain where there is a difference between gcc and g++?
You probably forgot to put
#ifdef __cplusplus
extern "C" {
#endif
near the beginning of your libiec60063.h header file, and
#ifdef __cplusplus
}; // end extern "C"
#endif
near the end of your header file, or if you don't want to change the header file:
extern "C" {
#include <libiec60063.h>
};
in your C++ code.
See this question. You need to disable name mangling. Read about compatibility of C & C++ .
BTW, you should compile with g++ -Wall -Wextra -g and perhaps with -std=c++11 and code for C++11
(hopefully) quick question that I can't find the answer to:
I have been given a brief assignment in C++. We are to write a 3-file program. There will be a function file, a header file, and a driver file. Here's what I've got so far:
Header (test.h):
#include <iostream>
using namespace std;
#ifndef TEST_H
#define TEST_H
int foo (int bar);
#endif
Function (test.cpp):
#include <iostream>
#include "test.h"
using namespace std;
int foo (int bar){
bar++;
}
Driver (drive.cpp):
#include <iostream>
#include "test.h"
using namespace std;
int main(){
int x = foo(2);
cout << x << endl;
return x;
}
When I try to compile drive.cpp, I get the following error:
drive.cpp:(.text+0xe): undefined reference to `foo(int)'
So...what am I doing wrong?
For a small project like this, simply compile all .cpp files at once:
g++ main.cpp driver.cpp
For a larger project, you separate the compile and link steps:
compile:
g++ -c main.cpp -o main.o
g++ -c driver.cpp -o driver.o
link:
g++ main.o driver.o
Or rather, you'd have a makefile or IDE do this for you.
In drive.cpp, instead of
#include <test.h>
make it
#include "test.h"
This is the variant of #include syntax that is used for header files of your own program (not system header files). When you use this version the preprocessor searches for include files in the following order:
In the same directory as the file that contains the #include statement.
In the directories of any previously opened include files in the reverse order in which they were opened. The search starts from the directory of the include file that was opened last and continues through the directory of the include file that was opened first.
You need to do one of two things:
Compile all the files at once
# replace 'driver.exe' with what you want your executable called
g++ -Wall -ggdb -o driver.exe main.cpp driver.cpp
Compile all the files to object files and then link the object files:
# again, replace 'driver.exe' with what you want your executable called
g++ -Wall -ggdb -o main.o -c main.cpp
g++ -Wall -ggdb -o driver.o -c driver.cpp
g++ -Wall -ggdb -o driver.exe main.o driver.o
As a side note, you should probably change
#include <test.h>
to
#include "test.h"
and putting "using namespace std;" in a header file is going to cause you copious grief later on.
in test.cpp, change the return line to this:
return bar++;
I have a dependancy library (libfcgi) that I compiled with g++ (GCC v4.4 MinGW) using the following calls:
g++ -Iinclude -c -O2 *.c
ar rcs ../libfcgi.a *.o
Now, my main project is built like so:
g++ -Idependancies\libfcgi\include -Ldependancies -O2 -lfcgi *.cpp
g++ apparently finds libfcgi.a, but yet it fails to link to the following references:
'FCGI_printf'
'FCGI_Accept'
In the libfcgi sources, these functions are defined as follows:
#ifdef __cplusplus
extern "C" {
#endif
//...
DLLAPI int FCGI_printf(const char *format, ...);
DLLAPI int FCGI_Accept(void);
//...
#ifdef __cplusplus
}
#endif
where DLLAPI is nothing (as it isn't compiled as a shared library) and __cplusplus is defined (g++).
Looking at libfcgi.a, those functions are exported as '_FCGI_Accept' and '_FCGI_printf', so with an underscore in front. That's what seems to hinder g++ to find them.
I thought using export "C" would suffice to link to a C function in C++, so what am I doing wrong?
Thanks :)
If you have the same extern "C" definitions in your .cpp sources, then I think your problem is that the -lfcgi should follow the *.cpp in your command line:
g++ -Idependancies\libfcgi\include -Ldependancies -O2 *.cpp -lfcgi
In your main-project, you tell the compiler to link C-functions, due to the extern "C". It therefore expects unmangled symbol-names. You should therefore compile the fcgi-library with the C compiler, not the C++ compiler.