C++ Quantlib EXC_BAD_ACCESS in Xcode - c++

I've been trying to run some of the example code (BermudanSwaption) in Xcode but keep getting an EXC_BAD_ACCESS code=2.
But compiling and running the BermudanSwaption code in the terminal works correctly.
The following code throws the same error in Xcode.
#include <ql/quantlib.hpp>
#include <iostream>
using namespace QuantLib;
int main (){
Date date(18, March, 2014);
std::cout << date << std::endl;
}
And this runs correctly.
g++ -I/opt/local/include/ -I/opt/local/include/boost main.cpp -o main -L/opt/local/lib/ -lQuantLib
Are there some specific settings I need to tweak in Xcode in order for this to run?

I found that #including the individual libraries separately rather then including ql/quanlib to work. So in my example sub ql/quantlib.hpp with ql/time/date.hpp and everything runs fine.

Following the suggestion of Kaush, and a bit of trial and error, the following works for me in QuantLib 1.4, and means you don't have to add in every single individual library:
In <ql/experimental/models/all.hpp>, comment out these lines:
#include <ql/experimental/models/kahalesmilesection.hpp>
#include <ql/experimental/models/markovfunctional.hpp>

Related

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

"undefined reference to `WinMain#16'" Error in gcc editor

I am just learning c++ and began to watch a youtube tutorial by thenewboston. Unfortunately he is using Code::Blocks while I am using gcc and I do not have the option to create new class files with a button click and so had to manually create them.
I dont understand why the same code in Code::Blocks and gcc will work in Code::Blocks but not gcc. Does gcc require different coding for the same language?
EDIT: I have downloaded and tested in Code::Blocks myself
Other questions talk of how I need to give windows an entry point, but I dont know how to do that.
Test.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
int main() {
ClassTest bo;
}
ClassTest.h Code:
#ifndef CLASSTEST_H
#define CLASSTEST_H
class ClassTest {
public:
ClassTest();
};
#endif // CLASSTEST_H
ClassTest.cpp Code:
#include <iostream>
#include "ClassTest.h"
using namespace std;
ClassTest::ClassTest() {
cout << "blah blah" << endl;
}
I'm not quite sure I understand what the question is; I'm going to take it as "how do I get these three files to build into a .exe that I can run from the Windows commmand line?"
The answer is to run something like this on the command line, in the folder with the files:
g++ -c Test.cpp -o Test.o
g++ -c ClassTest.cpp -o ClassTest.o
g++ Test.o ClassTest.o -o Test.exe
The first two commands build each CPP file into an "object file", which isn't a whole program by itself but which contains the compiled version of the code in that CPP file. The last command tells the compiler to paste together the two object files into a program, and resolve all the cross-references between them. (For example, the part where Test.cpp constructs a ClassTest object needs to end up calling the ClassTest constructor code from ClassTest.cpp.)
Code::Blocks is an IDE and works out how to build each source file in your project and link them together by itself. But if you aren't using an IDE, you need to do that in another way. You can either do it manually like this, or you can write a Makefile that will check which code files have changed and rebuild and re-link everything that depends on them when you run the make command, which is how most people do it.
As for "giving Windows an entry point", that probably refers to GUI applications that want to display windows on the screen. For console programs like the one you have written, the "entry point" is main(), and you just print stuff to the command line window. To make actual Windows-style GUI windows of your own, you need to use the Windows API, which I can't tell you much about.

Eclipse CDT does not resolve variables initialized in #include files

The problem occurs when trying to use CDT for editing files in OpenFOAM, a popular computational fluid dynamics software package. I am using Eclipse Kepler SR2 Build id: 20140224-0627 and CDT 8.3.0.201402142303 The following is a simplified version of the problem.
Given the following hello.cpp
#include <iostream>
#include <string>
int main(){
#include "hello.H"
std::cout << hello << std::endl;
}
and the following hello.H
std::string hello("Hello world!");
Indexing can't resolve hello in hello.cpp. However, if I change hello.H to hello.inc it does work. This isn't a feasible solution for OpenFOAM. How could I make the indexing and autocomplete work with the given structure?
Note: I did try Eclipse Luna too, and the same happened.
Edit: I think this might be a bug. After more experimenting, the following happened. When I named the hello.H to hello.inc, the hello in hello.cpp was recognized. However, when I rebuild index it wasn't. Then I resaved hello.inc without functional changes, and behold, it was recognized again. However, if I turned off automatic indexing it no longer got resolved after rebuilding the index.
Edit 2: After further research on the problem, I found the following duplicates, without the random behavior:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=418085
and to my shame:
#include inside function body doesn't work (CDT/Eclipse C++)
Thus this question can be closed as a duplicate. To those interested, CDT indexer is not actually designed to do this, as this is bad coding style. The main message to me was that I should use some other IDE with OpenFoam.

SFML crashes at first call using Code::Blocks

I am using SFML 1.6 with Code::Blocks 12.11 on a windows 8.1 computer. I have been having problems so I made a very simple test program, which looks like this:
#include <iostream>
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
int main(){
std::cout<<"Start"<<std::endl;
sf::Sprite test;
std::cout<<"End"<<std::endl;
return 0;
}
When I try to run this it prints Start, then crashes. I have checked my linker settings and I think they are right because it compiles just fine with no errors or warnings. Is there something I'm missing?
I actually missed the fact that you're running SFML 1.6. I've tried your code with the latest build from GitHub and it runs just fine. Either this is some bug in 1.6 or you're doing something wrong somewhere else (you shouldn't see any program window unless you're creating one yourself).
Try downloading the latest version (2.1) from the downloads page and see whether it crashes as well. Right now I guess it crashes due to the incompatibility mentioned in this question/answer. Try running g++ -v from the command line to determine the exact version of GCC you're running.

Beginner working with objects and classes getting the following errors

This is a tutorial I've been following and I've done everything that it tells but it doesn't work. I have three files: the main.cpp, burrito.h (the class), and burrito.cpp.
And here are the three files respectively.
main.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
int main() {
Burrito bo;
return 0;
}
Burrito.h
#ifndef BURRITO_H
#define BURRITO_H
class Burrito {
public:
Burrito();
};
#endif // BURRITO_H
Burrito.cpp
#include <iostream>
#include "Burrito.h"
using namespace std;
Burrito::Burrito() {
cout << "Hello World" << endl;
}
When I build and run I get the following error:
...undefined reference to `Burrito::Burrito()'
collect2: ld returned 1 exit status
Process terminated with status 1 (0 minutes, 6 seconds)
1 errors, 0 warnings
I'm compiling using CodeBlocks.
I'm using CodeBlocks
This is the issue.
If you’re starting to learn C++ it’s (unfortunately) essential to learn about translation units. An IDE like Code::Blocks hides this detail from you – and does it wrong, in this case (this isn’t really the fault of Code::Blocks though, it can’t automatically guess what to do in this case without configuation).
In the beginning, drop the IDE, go to the command line for compiling. Compile the two translation units separately and link them together explicitly.
g++ -o burrito.o burrito.cpp
g++ -o main.o main.cpp
g++ -o main main.o burrito.o
Every good beginners’ C++ book will explain how this works.
When you're linking objects together to get the final executable file you're forgetting to link the compiled-object from burrito.cpp file correctly
If you're building with a Makefile, your final output rule should have something like "-o main main.o burrito.o"
Using Code Blocks 13.12 I right clicked on the Burritto.cpp file chose Properties, then chose the Build tab and checked the compile file and link file check boxes then clicked ok saved everything then ran and it worked great.