I'm working on a project that has three cpp files and two header files
All my files are in the same folder.
I've been trying to link the header files with the cpp files for the past few days and I'm struggling so much with it.
Firstly, I tried to add the files in Build Options > Search directories > Compiler > Add
Image of my setting in Build options
I've ensured that my header files are typed out properly:
vectorfunc.h
#include <vector>
#ifndef VECTORFUNC_H_INCLUDED
#define VECTORFUNC_H_INCLUDED
//my vector functions declaration
#endif // VECTORFUNC_H_INCLUDED
vectorfunc.cpp
#include <vector>
#include "vectorfunc.h"
#include <iostream>
//my function definitions
TrackKalman.h
#ifndef TRACKKALMAN_H_INCLUDED
#define TRACKKALMAN_H_INCLUDED
void TrackKalman();
#endif // TRACKKALMAN_H_INCLUDED
TrackKalman.cpp
#include <valarray>
#include <vector>
#include <iostream>
#include "vectorfunc.h"
//function definition of TrackKalman()
main.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <cmath>
#include "TrackKalman.h"
//main function
Despite all this I still get the error in my compiler:
||=== Build: Debug in RadarScanner (compiler: GNU GCC Compiler) ===|
files\RadarScanner\TrackKalman.cpp -o obj\Debug\TrackKalman.o||No such file or directory|
files\RadarScanner\main.cpp -o obj\Debug\main.o ||No such file or directory|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 2 second(s)) ===|
I'm using the GNU GCC Compiler
Any help would be greatly appreciated. I can't seem to fix the issue and I definitely don't want to make the whole program in one file.
I think you are missing the
#include "TrackKalman.h”
As first line in TrackKalman.cpp
Related
I have an existing project written on C++ that I would like to compile for webassembly using emscripten. The code calls boost library:
#include <boost/program_options.hpp>
#include <iostream>
#include <string>
#include <exception>
#include <algorithm>
#include <iterator>
#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/exception.hpp>
#include <boost/filesystem/convenience.hpp>
#include <boost/thread/thread.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/assign.hpp>
I have compiled the necessary parts of boost library using emscripten as static libraries and converted them from bc to a-files using emar. Now I'm trying to compile the project feeding the compiler with the precompiled libraries:
(part of Makefile)
C_OPTIONS= -O3 -DNDEBUG -g \
/home/hiisi/workspace/boost_libs/program_options/build/emscripten-1.38.38/release/link-static/threading-multi/libs/cmdline.bc.a \
/home/hiisi/workspace/boost_libs/program_options/build/emscripten-1.38.38/release/link-static/threading-multi/libs/config_file.bc.a \
/home/hiisi/workspace/boost_libs/program_options/build/emscripten-1.38.38/release/link-static/threading-multi/libs/convert.bc.a \
/home/hiisi/workspace/boost_libs/program_options/build/emscripten-1.38.38/release/link-static/threading-multi/libs/libboost_program_options.bc.a \
However make still complains on the very first occurrence of boost in the code:
main.cpp:1:10: fatal error: 'boost/program_options.hpp' file not found
#include <boost/program_options.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
shared:ERROR: compiler frontend failed to generate LLVM bitcode, halting
The question may sound little bit naive, but how do I correctly do this? The project compiles perfectly fine with g++, but not em++
All I had to do is to make sure that boost lib presents in emscripten include directory. In my case that was emsdk/fastcomp/emscripten/system/include/ I made a symlink to system' boost library there and everything worked like a charm.
You have to add the include directories to use boost.
That would be an argument that look like this:
... -I/home/hiisi/workspace/boost_libs/include ...
I'm using Code::Blocks 17.12 with the default MinGW GCC C++ compiler (with -std=c++11) on Windows 10. I have built several C++ console projects without issues. However, when I include the STL priority_queue class, I get the following error:
fatal error: priority_queue: No such file or directory
#include <priority_queue>
^
compilation terminated.
Process terminated with status 1 (0 minute(s), 0 second(s))
1 error(s), 0 warning(s) (0 minute(s), 0 second(s))
These are my #include directives:
#include <queue>
#include <vector>
#include <priority_queue>
#include <iostream>
Any help would be appreciated.
http://en.cppreference.com/w/cpp/container/priority_queue
Defined in header <queue>
let me start by saying that I am not the best C++ and I know very little in ways of Linux. For a class project we had to implement a heap so I coded everything on my Windows PC assuming I could just upload the files to the Linux repository the school has. [Perhaps this is where I went wrong and this cannot be done this simply.] My codes compiles and clears all test cases provided on my Windows pc. When I upload the files to Linux, I created a makefile and when I use the make command I get back a laundry list of multiple definition errors. One error per function that I am using. I have done some searching and I am more confused then when I started.
My files are: main.cpp, main.h, heap.cpp, heap.h, util.cpp, and util.h.
I think the issue is with my include statements but I am not 100% sure.
Here is an example of the files.
main.cpp
#include <iostream> //needed to use basic inputs/outputs
#include <stdio.h>
#include <stdlib.h>
#include "main.h"
#include "util.cpp"
#include "heap.cpp"
#include <fstream>
using namespace std;
main.h is blank.
heap.cpp
#include <iostream> //needed to use basic inputs/outputs
#include <stdio.h>
#include <stdlib.h>
#include "heap.h"
#include <cmath>
#include <math.h>
using namespace std;
//expanded functions found in the heap.h file
heap.h
//2 structs
//9 functions
util.cpp
#include <iostream> //needed to use basic inputs/outputs
#include <stdio.h>
#include <stdlib.h>
#include "util.h"
using namespace std;
//expanded functions found in util.h
util.h
//1 function
Between heap.h and util.h I have 10 function and upon running the make command I get a warning about all ten:
multiple definition of 'FUNCTIONNAME'
main.o:main.cpp:(.text+0x1b7): first defined here
I am assuming the 0x1b7 is a memory location because they are each different.
Any help would be greatly appreciated.
You haven't shown the Makefile, but most likely, it includes this or a similar rule
program: main.o heap.o util.o
$(CXX) $(CXXFLAGS) -o program main.o heap.o util.o
What happens now, the compiler builds the three object files main.o, heap.o and util.o. Next the object files are linked together to build program.
The linker sees definitions of the various functions defined in both main.o and heap.o, or main.o and util.o respectively. This is why it complains about "multiple definition of 'FUNCTIONNAME'"
Why are these functions defined more than once?
When you include a file into another source, it is as if you copy the contents at the location of the #include. This means a function defined in heap.cpp:
void create_heap(int size)
{
// create a heap ...
}
is copied verbatim into main.cpp where the line
#include "heap.cpp"
is.
Because heap.cpp has the definition of create_heap() and main.cpp #includes the contents of heap.cpp, both contain their own copy of create_heap(). Now you compile both heap.o and main.o, and link them together. Each object file has a copy of create_heap() and this is where the linker is confused and complains
multiple definition of 'create_heap'
main.o:main.cpp:(.text+0x1b7): first defined here
To fix this, simply replace the lines including the cpp sources, e.g.
#include "util.cpp"
#include "heap.cpp"
with their respective header files
#include "util.h"
#include "heap.h"
Only keep the function definitions relevant to main.cpp, nothing else. Now main.cpp has no function definitions belonging to util.cpp or heap.cpp, and the linker errors are gone.
Presumably, this worked on Windows, because only main.cpp was included in the project file, and so only one definition (from main.o) was in the resulting executable.
If you had included all sources as in the Linux Makefile, the error could be seen in Windows as well.
These are the header files and libraries
#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <math.h>
#include <vector>
#include <numeric>
#include <strings.h>
#include <assert.h>
#include <dirent.h>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/geometry.hpp>
#include <boost/geometry/geometries/point_xy.hpp>
#include <boost/geometry/geometries/polygon.hpp>
#include <boost/geometry/geometries/adapted/c_array.hpp>
#include "mail.h"
I am trying to compile this c++ file I got using
g++ -O3 -DNDEBUG -o evaluate_object evaluate_object.cpp
The compile output
evaluate_object.cpp:13:10: fatal error: 'boost/numeric/ublas/matrix.hpp' file
not found
#include <boost/numeric/ublas/matrix.hpp>
^
1 error generated.
Ps: I run on macos sierra 10.12
I have tried install boost by these ways:
sudo port install boost
brew install boost
sudo port install boost +universal
But I still got the same problem.
I would be happy to get out of this noob zone.
I am a macos newbie too.
It seems that your "boost" includes directory is not present within environment defined include directories. You might want to specify one by passing "-I" argument to g++ command, i.e.
g++ -O3 -DNDEBUG -I/usr/local/Cellar/blahblah -o evaluate_object evaluate_object.cpp
Also your code is incomplete and perhaps if you actually call some of the boost functions you might need to add some objects for linking and then you will face "undefined reference to" error. To overcome such you need to specify "-L" flag for your g++ command.
On the How-To initialize libosip2 site theres program to initialize osip libraries
https://www.gnu.org/software/osip/doc/html/group__howto0__initialize.html
#include <winsock2.h>
#include <Time.h>
#include <stdio.h>
#include <stdlib.h>
#include <osip2/osip.h>
#include <osipparser2/osip_parser.h>
#include <string.h>
int main()
{
int i;
osip_t *osip;
i=osip_init(&osip);
if (i!=0)
return -1;
}
I'm trying to run this code but it doesnt work,
library version 5.0.0
error:
||=== Build: Debug in cos2 (compiler: GNU GCC Compiler) ===|
obj\Debug\main.o||In function `main':|
C:\Users\emergency\Documents\analizer\cos2\main.cpp|14|undefined reference to `osip_init'|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
In order to link with a library, you need to specify it on the command line.
Note that libosip2 produces two libraries and you have to link with both of them in order to use both parser (libosipparser2) and the sip engine (libosip2).
The exact command line depends on the platform, compiler you use and may also differ if you are linking to a static library or dynamic library.
With GCC and dynamic linking, it should be that way:
-L/install-directory-for-libosip2-libs/ -losipparser2 -losip2
-L/install-directory-for-libosip2-libs/ parameter refers to the directory where libraries are available.
this is because you did not link the binary osip.o