accessing functions declared in some other file using #include in c++ - c++

I have a file EnabledSets.cpp in which I am calling a function in following form
ChessImpl::SetEventAttribute(ec.getEventId(sid-1), DISABLE, transition.tid);
this I am doing in file named enabledSet.cpp in which I include ChessImpl.h
in chessImpl.h I am including Chess.h which in turn defines the function in Chess.cpp
but when I am running my code I get this error
EnabledSets.cpp $(.text+0xa0f): undefined reference to `ChessImpl::SetEventAttribute(EventId, unsigned int, unsigned int)'
Thanks in advance

This isn't java, you need to pass gcc ALL the .cpp files at once, or compile and link separately.

When you write a command like g++ file_name.cpp you're telling GCC to compile and link that file into a complete program. It fails because that single file isn't a complete program; parts of the program are in other source files.
You can either compile everything at once with:
g++ file1.cpp file2.cpp -o myprog
or you can compile each file individually (into a .o file) and then link them as a separate step:
g++ -c file1.cpp
g++ -c file2.cpp
g++ file1.o file2.o -o myprog
The first way is simpler if you're just typing commands at a prompt, but it's very inefficient when working on a large program since it recompiles everything, even if very little has changed. Large applications typically have build systems that compile source files individually, and only recompile the ones that have changed.

Related

how can I not create precompiled header when compile object file?

I use g++ 10.2.0 and try to create a static library, but when I create object file for archiving a static library, object file format always shows precompiled header, it makes the final static library cannot work:
//file static_test.cpp
void fn(){
int temp;
++temp;
}
//file static_test.h
void fn();
build them but not link
g++ -c static_test.h static_test.cpp -o static_test.o
use file to show static_test.o format
file static_test.o
static_test.o:GCC precompiled header (version 014) for C++
and I archive it
ar rsv libstatic_test.a static_test.o
use file to show libstatic_test.a format:
current ar archive
use a main.cpp to test this static library
#include "static_test.h"
int main(){
fn();
return 0;
}
compile them and link
g++ main.cpp libstatic_test.a
libstatic_test.a: cannot add symbol: archive has no index;run ranlib to add one
collect2: error:ld return 1
why and how to solve this problem, tks~
-c is only for a single file, the second static_test.cpp is ignored. You should get the compiler warning about multiple files set to -c. g++ -c static_test.h results the precompiled header in static_test.o and static_test.cpp is ignored. The proper command should be
g++ -c static_test.cpp -o static_test.o
Do not pass header files to the compiler when you compile object files. All other commands you are using look ok.
if you would like to create a static library with gcc, you have to say it to the linker/wrapper programm "gcc" like:
gcc -static -o libyourlibname(.lib/.so) obj1.o obj2.o -s
legende:
-static: tells the linker to build a static lib
-o : output file
-s : strip all debug/linking stuff, including debug informations
note:
may be you need the option -fPIC at .c compile time like:
gcc -O2 -fPIC -c file1.c -o file1.o
legende:
-O2 : tells the c compiler to optimize
-fPIC : create program independet code (internal for the output code)
-c : compile C file to object file:
-o : tell the linker how the object file should be named
By the way:
Pre-compiled header files are only created by compiling C/C++ files only.
You have require huge memory, and mostly pre-compiled header files are not needed in small projects of small student homework tasks.
And each time you change the header file, you (the compiler) have to create a new copy of the .pch file.
Of course, .pch files are good for end-products which does not change it in the form for the developer. But they are mostly depend on the compiler.
So, you can't use .pch files from Windows MinGW64 Project under Linux (with the near) same compiler in different versions.

Compiling C files along with C++ file in g++

I'm having a custom C header file that I have created. There are several files in my directory as follows.
lib1/
-lib1.h
-lib1.c
lib2/
-lib2.h
-lib2.c
-lib_main.c
-lib_main.h
-main.c
-main.cpp
-Makefile
Now, for testing the header file with a test file called main.c, I will be giving the following command in the terminal,
gcc lib_main.c lib1/lib1.c lib2/lib2.c main.c -o ./main
Now, how could I test the same header files with main.cpp instead of main.c, how do I change it?
You should (and most probably must) compile separately the c and c++ sources into a object file, and then link together.
As an example
gcc -c -o lib1.o lib1/lib1.c
gcc -c -o lib2.o lib2/lib1.c
gcc -c -o lib_main.o lib_main.c
g++ -c -o main.o main.cpp
g++ -o main lib1.o lib2.o lib_main.o main.o
The first four commands create a series of object files, with extension .o. The last command link together the object files into an executable.
Obviously you need to add the relevant compiler options for each source.
Two important points to notice:
Order of files in the linking is important. See discussion here and here.
To mix c and c++ code with each other, for example if a c++ code calls a c function, you need to follow specific guidelines.

how to run c++ file if header, class, and main are not in the same folder?

The code::block IDE generates the following files:
./main.cpp
./include/class.h
./src/class.cpp It include class.h with #include "class.h"
How can I run this set of files, with the three files in three different folders?
First, this program can be run by clicking IDE "build and run" button.
This program need to take some arguments, like ./a.out arg[1] arg[2]. So I cannot input arguments by clicking "build and run" button, and thus I have to use g++ to compile an output first.
But g++ is not smart enough as the IDE in finding the three files(I try g++ -I./include main.cpp, it seems that it has no problem with class.h file, but cannot find class.cpp file)
So how can I compile the three files in three different locations?
BTW, how could the class.h file find the class.cpp file in IDE/g++(scan all the files in the directory to see which contains the definition of the class functions?)?
It's a bad idea to #include source files. But this will do it:
g++ -I./include -Isrc main.cpp
Normally one would expect that the IDE has some function to just build the application, especially when there's a function to build-and-run. In addition there are those that have the possibility to supply command line arguments for the program so build-and-run will run with supplied arguments.
You have to supply the source files and the search path for includes, normally one would write:
g++ -o exec-file-name -I./include main.cpp src/class.cpp
but that may depend a bit on how you include the header file. Another note is that you normally don't compile the header file separately - it's included when you compile the .cpp files that includes it.
If on the other hand you actually want to do what you write (compile the .h file that includes the .cpp file - which is higly unorthodox) you would do:
g++ -c -I./src include/class.h
g++ -c main.cpp
g++ -o exec-file-name main.o class.o
where you need to replace the .o extension if your platform uses another extension. Note that in this case you should probably not include class.h from main.cpp since that could lead to duplicate symbols.

Trouble making a header file

I am trying to make my own header file which will contain a class Vector.
To create a header file, as advised by some websites, I have created two different files :-
1) collections.h (contains declarations)
2) collections.cpp (contains definition)
Another file is main.cpp, which contains the main function, and in which I will be using collections.h
All these files are in the same directory
The trouble I am having is that compiler is showing the error
Undefined reference to Vector::Vector(int, int)
and so on for all the functions in my class.
I have made sure that there is a #include "collections.h" line in both collections.cpp as well as main.cpp
How can I solve the above problem?
I am using gcc compiler under ubuntu 12.04
First build the object files:
gcc -c main.cpp -o main.o
gcc -c collections.cpp -o collections.o
then link them together:
gcc collections.o main.o -o main
You need to build both source files and link them together.
On a Linux command line, you can do it simplest by providing both source files to gcc:
$ g++ -Wall main.cpp collections.cpp -o my_program
Note: I added the -Wall option to enable more warnings by default. It's always a very good habit to enable more warnings, as they can often point out logical errors or possible places where you have undefined behavior.

Linking files in g++

Recently I have tried to compile a program in g++ (on Ubuntu). Usually i use Dev-C++ (on Windows) and it works fine there as long as I make a project and put all the necessary files in there.
The error that occurs when compiling the program is:
$filename.cpp: undefined reference to '[Class]::[Class Member Function]'
The files used are as following:
The source code (.cpp) file with the main function.
The header file with the function prototypes.
The .cpp file with the definitions for each function.
Any help will be appreciated.
You probably tried to either compile and link instead of just compiling source files or somehow forgot something.
Variation one (everything in one line; recompiles everything all the time):
g++ -o myexecutable first.cpp second.cpp third.cpp [other dependencies, e.g. -Lboost, -LGL, -LSDL, etc.]
Variation two (step by step; if no -o is provided, gcc will reuse the input file name and just change the extension when not linking; this variation is best used for makefiles; allows you to skip unchanged parts):
g++ -c first.cpp
g++ -c second.cpp
g++ -c third.cpp
g++ -o myexecutable first.o second.o third.o [other dependencies]
Variation three (some placeholders):
Won't list it but the parameters mentioned above might as well take placeholders, e.g. g++ -c *.cpp will compile all cpp files in current directory to o(bject) files of the same name.
Overall you shouldn't worry too much about it unless you really have to work without any IDE. If you're not that proficient with the command line syntax, stick to IDEs first.
The command line of gcc should look like:
g++ -o myprogram class1.cpp class2.cpp class3.cpp main.cpp
Check in which cpp file the missing class member function is defined. You may have not given it to gcc.
You can also check for correct #include tags within filename.cpp. Assume that filename.cpp uses code contained in myclass.h present in the same directory as filename.cpp. Assume that the class that g++ says is undefined is contained in myclass.h and defined in myclass.cpp. So, to correctly include myclass.h within filename.cpp, do the following:
In filename.cpp:
#include <iostream>
#include <myclass.h>
//..source code.
In the makefile:
filename.o: myclass.C myclass.h filename.cpp
g++ -I./ -c filename.cpp -o filename.o
myclass.o: myclass.C myclass.h
g++ -c myclass.C -o myclass.o
In the above, note the use of -I. option when compiling filename.cpp. The -I<directory> asks g++ to include the path following the -I part into the search path. That way myclass.h is correctly included.
In the absence of more information (the source maybe), it is difficult to say with any accuracy where the problem lies. All attempts will be but stabs in the dark.
I assume that you have declared a member function (usually in a .h or .hpp file) but have ommited the respective definition of the member function (usually in a .cpp file).
In c++, it is possible to declare a class like so:
class foo {
void x();
void y();
}
with a cpp file that goes like so
void foo::x() {
do_something()
}
Note, there is no foo::y().
This poses no problem to the compiling/linking process as long as the member function foo::y() is referenced nowhere throughout the compiled code.