Undefined reference to Eclipse C++ - c++

I've tried more and more time, but I got the same error.
When I use an external lib, for example I'm try to work on openssl and crypto++, I receive the error: "undefined reference to (function)".
I dunno why Eclipse do that neverthless the #include are all correct.
I've tried even to add the path from Properties-C/C++ General-Path & Symbols but nothing.
Can anybody help me?

You must specify the name of the shared libraries which must be linked to the executable.
Assuming you are using the GNU toolchain, you can do it by following these steps:
Right-click on the project, then select Properties
Go under C/C++ Build -> Settings
Select GCC C Linker -> Libraries
In Libraries (-l), add the name of your libraries,
If needed, put the directory where your libraries are hosted in Library search path (-L).

I have the same problem too. I was using Eclipse CDT and trying to build my source code with OpenSSL headers and I got the same "undefined reference" problem.
To those who may be also suffering from this type of errors, try these steps:
Make sure you use the right compiler(for Ubuntu 18.04)
right click your project->preference->C/C++ Build->tool chain editor->use CDT internal Builder / Linux gcc(you can try to run HelloWorld as a test)
C/C++ Build->settings->gcc linker, see right there is a "+", click and type ssl and crypto
Rebuild your project, done.
I strongly recommend you to learn gcc compiler command line. If you have compile problem, always use the terminal and command line to see if you can compile it successfully, then compare the succeeded command with the console log in Eclipse, at where you can see the actual gcc command that used to compile your code.
A good resource for gcc command: https://www.rapidtables.com/code/linux/gcc/gcc-l.html

Related

How to compile Quantlib via Xcode?

I am trying to install QuantLib on my Mac running OSX 10.11.6. Installed Boost 1.59 via MacPorts and then followed these instructions.
I used these additional environment variables
./configure --with-boost-include=/opt/local/include/ \
--with-boost-lib=/opt/local/lib/ --prefix=/opt/local/ \
CXXFLAGS='-O2 -stdlib=libstdc++ -mmacosx-version-min=10.6' \
LDFLAGS='-stdlib=libstdc++ -mmacosx-version-min=10.6'
and then make && sudo make install.
However when I run the Bermuda Swaption test it gave me the same error described here.
Little premise: I don't know anything about C++. I need QuantLib to work on Python. So I read carefully the answer by SmallChess and tried to solve it by myself. As I read in his answer
You can't just compile BermudanSwaption.cpp and hope everything would be fine. You have to compile the entire QuantLib library and link with the generated library files. Please google "compiling and linking C++" for more information.
By far, the easiest way to make it happen on Mac is to do it with Xcode. You will need to create a new Xcode project, and import the entire Quantlib project files into it. Next, you will need to create a main() function. Xcode does the compiling and linking for your automatically.
This is what I exactly did:
created a new project in Xcode (version 8.2.1)(file/new project/Command Line Tool/"HelloWorld"/Documents/create)
selected Targets, Build Phases and Link Binary With Library. Added libQuantLib.0.dylib
set libstdc++(GNUC++ standard library) as C++ Standard Library in Build Settings
Modified Header Search Paths to include: /opt/local/include/, and Library Search Paths to include: /opt/local/lib
C++ Language Dialect is set on Compiler Default.
Dragged the ql folder onto the left window of the Xcode
Now, I managed to copy a simple code which includes the library and even if there are many warnings, it runs. Still when I run on the Terminal the command for the Bermuda Swaption test I get the same error. What am I doing wrong?
Additional info (may or may not be useful): if I change the C++ Standard Library setting on Xcode to libc++, I get on Xcode the same error I get when i try the Bermuda Swaption test (ld: symbol(s) not found for architecture x86_64).
Any help would be very much appreciated
Regards
EDIT: you can find a picture of the code at https://i.stack.imgur.com/1zhjO.png

Compiler flags in Eclipse

My build requires that I issue the following commands:
$ g++ sniff.cpp -o sniff -lcrafter
However, in my Eclipse build, all the complier gets is:
g++ -o "sniffer_crafter" ./src/sniffer_crafter.o
After getting these commands it complains that I have an undefined reference to the library Crafter.
How can I resolve this linking issue using Eclipse? I have seen others answers to similar questions, but they don't seem to address Eclipse's current layout. I'm using the most recent edition of Eclipse Kepler.
Include libraries:-
right click on the project -->
Goto Properties
then goto --> C/C++ Build --> Settings
There you will find Linker and sub type Libraries.
Add the library path in Library Search Path on right hand side (where your .so file is located) and give the lib name in libraries
for libcrafter.so path --> /opt/myLib/lib
give path as /opt/myLib/lib
and library name as crafter

How to install protobuf on windows? (Win7x64/MinGW)

C++-Protobuf does not compile in VS2012. Now I want to use MinGW to compile it on windows. Can someone please give me some brief headwords on how to compile protobuf on Win7 x64. I already installed MinGW with the GUI installer. Google writes as MinGW setup notice that I should refer to the Unix installation notes. But I cant figure out how to use the auto tools on windows.
Edit
Okay this is what I've done until now:
$ mount C:/ WinDir
$ cd ./[...]/protobuf.2.4.1
$ ./configure
$ minGW32-make.exe
$ minGW32-make.exe check
minGW32-make.exe runs without errors, but no tests are running and I cant find libprotobuf.lib. There are some libprotobuf.dll but I need the lib, dont I?.
You should have an MSys console together with your MinGW instalation. This console provides an linux-like environment in which you should be able to use autotools normally.
If MSys is not installed, you can grab it from the MinGW site too.
cd to your directory with sources and try the usual:
$ ./configure
$ make
Some libraries cause problems on Windows but most compile well with MinGW and MSys. Come back and add more info to your question if you run into specific problems.
Edit:
minGW32-make.exe runs without errors, but no tests are running and I cant find libprotobuf.lib. There are some libprotobuf.dll but I need the lib, dont I?.
Usually for a dynamic library you'd get protobuf.dll (the dynamic library) and libprotobuf.a (the static wrapper library).
When linking, just pass -lprotobuf to the linker - it will look for both libprotobuf.a and protobuf.lib.
(.lib is another static library format, which is partially handled by MinGW but not native here.)
You will not work with a .lib file when using the MinGW toolchain. Instead, you are able to link against the dll directly. The MinGW Wiki explains this.
I could get dll and lib both. This is when you do not want static lib file and want to use dll and lib file.
You need to make following changes in Protobuf code:
Open the project in VS. Or any other editor. I use VS2015.
In libProtoBuf project settings, in C/C++ Preprocessor add following flags.
PROTOBUF_USE_DLLS; LIBPROTOBUF_EXPORTS;
Those flags will export information from profobuf using dllexport
in ur client code where you are using Protobuf, define: PROTOBUF_USE_DLLS. Which will make protobuf includes to use dllimport.
Once you do step 2, you will see both dll and lib in your output folder. Otherwise, you will always see just dll and not lib file.
Hope this helps. If not, please write a message here and I can help you getting this sorted out.

Installing Boost libraries with MinGW and CodeBlocks

I'm having my first fling with the Boost libraries, and I've picked a pretty girl named Regex.
I've installed the libraries (which build automatically?) on my machine, but I'm getting the above error (cannot find -lboost_regex). I'm using Code::Blocks with MinGW, and a C++0X compiler flag.
I have
Pointed the "search directories" to the installation directory
Added the -lboost_regex flag to the linker
but no luck. Can someone help me get this working?
Update
Got things running now. I've added some further notes in an answer below, for newcomers to this problem.
(Also, changed the title of the question since it turned out to be a broader issue than when I started out.)
Here's some links and tips that can help a newcomer, from my first build experience. I built the libraries directly from the zip file. I built on MinGW and I used CodeBlocks for the IDE.
Download Boost zip, unzip somewhere (I'll call that place $boostdir)
Pretty large when unzipped, > 300MB
Add MinGW bin to PATH var
When Boost builds, it will need access to MinGW executables
Build b2.exe and bjam.exe
The documentation for Windows blithely assumes MSVC compiler is available.
If it is, you can apparently use the bootstrap.bat like the docs say.
If it's not (like mine), you'll have to build the exe files yourself, in steps 4 and 5.
In CMD, navigate to $boostdir/tools/build/v2/engine
Run build.bat mingw (will build b2.exe and bjam.exe)
Some aging basic documentation on that
Now you've got b2 and bjam custom-built according to your system spec. Navigate back up to $boostdir and get ready to start building the libraries.
Boost will make a new bin.v2 directory in the current directory.
All the libs will go in bin.v2.
This is an "intermediate" directory, for some reason
Nothing to do in this step, just some extra info :)
Run b2 toolset=gcc --build-type=complete
This takes a long time, in the neighborhood of 1 - 2 hours.
You'll know if it's working. If you think something's wrong, it's not working.
The build can use various flags
Now you're all built. Time to set up CodeBlocks.
Point your compiler to the header files
Right click your project -> Build Options -> Search Directories tab -> Compiler tab -> add $boostdir address
Boost has built a DLL for the library you want according to your current system spec. Look in the stage\lib\ directory of $boostdir
This DLL will be used later in the linker, so don't close its explorer window yet
Mine was in C:\Program Files\Boost_1_52\stage\lib\libboost_regex-mgw44-1_52.dll
I think the documentation had a smart way to do this but I haven't tried it yet
The "intermediate" directory from step #6 can be deleted now that the build is finished
Point your linker to the directory of that DLL
Right click your project -> Build Options -> Search Directories tab -> Linker tab -> add
that directory address (blah\blah\blah\stage\lib\)
Add that DLL flag to your linker settings
Mine was -lboost_regex-mgw44-1_52
Deep breath, prayers to your god, and fire up a test.
Further docs that may either help or confuse:
The Code::Blocks website has a version of this that I didn't find until I neared the end of my search. It was fairly helpful but had a few weird things. This post also is helpful.
Good luck!
I'm not sure what you mean by which build automatically. Most of the Boost libraries are header-only, but a few, such as regex, need to be compiled to a shared / static library. The compilation step is not automatic, you need to invoke the Boost build system (bjam) to do this. Of course, there are sources (BoostPro for instance) that distribute pre-built Boost binaries for various platforms.
Once that's done, you need to add the path where the libraries are present to the linker's search path. For MinGW, this option is -L"path/to/library". Boost does have directives to allow auto-linking of the required libraries, and this seems to work pretty well with MSVC, but I've never gotten it to work with MinGW. So you must also list the libraries to be linked explicitly. The Boost libraries include target and version information in the file name by default, so a typical linker command line option will look like -lboost_regex-mgw47-mt-1_51 for MinGW gcc 4.7 and Boost 1.51

Installing Boost To Work With CodeBlocks

I am trying to install Boost so I can use it along with MinGW and Code Blocks. I'm running Code Blocks 10.05 and the version of MinGW that comes with the download on the Codeblocks website. I've downloaded Boost 1.49.0 from http://www.boost.org/. I've seen several different guides for installing Boost(building from source that is). However, none of them seem to play out nicely at all, with me getting stuck in the initial steps. I've attempted to run bootstrap.bat in the base directory /boost_1_49_0 and also in /boost_1_49_0/tools/build/v2.
Both yield a popup that tells me I'm missing mspdb100.dll. Do I need to get this dll somehow? Am I following the wrong procedure to install this?
When this error occurs, bootstrap.bat doesn't build the error log that it says so that is essentially useless. However, uninstalling Microsoft Visual C++ cause the dll error to disappear and the log file will generate. It shows that it attempts use msvc to build the project instead of gcc. So unlike the direction given in several guides, you must specify the toolkit at this point:
bootstrap.bat gcc
Then it will prompt you to run a file called b2 that can be built with the options (among others).
b2 --build-dir=C:\exampledir toolset=gcc
It appears in order for boost to be used that you have to explicitly add the files in <builddir>\boost\bin.v1\libs\system. Otherwise the you get an error like:
In function `_static_initialization_and_destruction_0':|
C:\removed\boost_1_49_0\boost\system\error_code.hpp|214|undefined reference to `boost::system::generic_category()'|
C:\removed\system\error_code.hpp|215|undefined reference to `boost::system::generic_category()'|
C:\removed\boost_1_49_0\boost\system\error_code.hpp|216|undefined reference to `boost::system::system_category()'|
The linker options can be found in Code Blocks by going to Project -> Build Options -> Linker Settings -> Add. Adding these libraries explicitly seems to be necessary even if you add the build directory to the linker Search Directories.