Segmentation Fault in CPXopenCPLEX - c++

I recently reinstalled a 32 bit version of CPLEX on a new Ubuntu 16.04. When I compile the following test program with g++-5.4, it crashes with a segfault.
#include "cplex.h"
#include <iostream>
using namespace std;
int main() {
int err;
cout << "CPX_VERSION: " << CPX_VERSION << endl << flush;
CPXENVptr env_ = CPXopenCPLEX( &err );
cout << "error: " << err << endl << flush;
if (env_) {
cout << "has env" << endl;
}
return 0;
}
I compiled this with the following call
g++ -m32 main.cpp -o main /path/to/cplex/lib/x86_linux/static_pic/libcplex.a -lpthread -I/path/to/cplex/include/ilcplex/
The output is
CPX_VERSION: 12060300
[1] 21609 segmentation fault (core dumped) ./main
Any suggestions of how to fix or debug this are welcome.
Edit:
The 64bit version works (without the compiler flag -m32 and using x86-64_linux instead of x86_linux in the path to the library, of course). But this doesn't solve my problem, as I need the 32bit version.
Edit2: I was able to get a working version on a different computer that also runs on my computer (I think this is a reason to exclude shared libraries as a possible source). I compared the strace output of both binaries and they were almost identical up until the error. The differences were the specific pointer values and the order of lines in the output (probably just an issue with the output buffer and not a real difference).
The working version does two calls to time(NULL) at the place were the other version segfaults. Since this is the first call to time in the output, this might be the issue.

I asked this question in the IBM forums and the end result seems to be that this is probably an incompatibility of an old CPLEX version and a new 32-bit binutils version. Since IBM stopped supporting 32-bit builds in the next CPLEX version, there is next to no chance that this bug will ever be fixed.

Related

Check file existence with fstream using x86_64-w64-mingw32-g++ for cross compilation

I have a program that is written in C & C++ and I'm rewriting some parts to improve it. This program has to work with files, but I am not able to check if a file exists on Windows with a cross-compiled exe.
To check if my code was the problem, I have created this simple example:
#include <fstream>
#include <iostream>
int main(){
std::fstream in_file("test.noexists", std::ios::in|std::ios::binary);
if (in_file) {
std::cout << "File exists" << std::endl;
}
else {
std::cout << "File doesn't exists" << std::endl;
}
}
Compiling this code using "x86_64-w64-mingw32-g++", version 8.3-win32 on Debian 10, I'm unable to check if a file exists because it always returns "true". To verify it I have compiled the same program on Linux using g++, and on Windows using mingw64, and both were working so looks like a problem building the project on Debian 10 mingw32.
I don't know if my problem is a bug on Debian version or I am doing something wrong. Anyone knows anything about it?
The only difference I have seen between both, is the version (8.1 on Windows vs 8.3 on Debian), and of course the SO.
Thanks!.
EDIT:
I have upgraded to Debian 11 which has the x86_64-w64-mingw32-g++ v10 and same behavior. I forgot to say that I am using Debian 11 on WSL, not a real Debian distribution. I don't think it matters.
EDIT2:
I have already seen the suggested thread, but the problem is that I want to use streams to manage the files and the only option in that thread also fails. Using any of the other three ways will be like a hack that will not help too much. I think that is not the best way to check the file presence with another example, and then reopen the file later to work with it.
Is supposed that the fstream class have boolean conversion as I have read in several threads and that is why I'm using this example. The Linux version works perfectly, so the function is working as expected. The problem is the Windows version compiled in Linux (cross compiled EXE).
I have tested in_file.good(), in_file.is_open(), in_file.bad(), just in_file... all of them returns like the file was open correctly. Also I have tried ifstream, but same behaviour.
Rest a bit always helps... I have found a workaround to my problem just reading 0 bytes.
#include <fstream>
#include <iostream>
int main(){
std::fstream in_file("test.noexists", std::ios::in|std::ios::binary);
char dummy;
if (in_file.read(&dummy, 0)) {
std::cout << "File exists " << in_file.tellg() << std::endl;
}
else {
std::cout << "File doesn't exists" << std::endl;
}
}
With this, even the cross compiled EXE works without problems.
I still thinking why only fails with the cross compiled EXE, but I was not able to find any info about it and how to fix it. At least this solution is working.
Best regards.

No output in c++-program if object is instantiated using Eclipse

I got an issue after installing Eclipse Kepler on my Windows 7 32bit machine. I installed the CDT and the MinGW-compiler. I configured the installation by adding MinGW to the PATH and tested my configuration with a "Hello world"-program, which worked.
The strange thing is, that if I instantiate an object, nothing is outputted. It doesn't matter if it's a std::string or a custom made class. If I instantiate it, nothing is outputted, even if it should be outputted before the instantiation. The exact same code works fine, if I compile it with cygwin gcc from command line. If I change the toolchain to cygwin gcc nothing changes (I've rebuild the program with "build all").
There is no error displayed and no problem listed.
Here's the minimal working example:
#include <iostream>
#include <string>
using namespace std;
class SayWorld{
public:
SayWorld(){
cout << "World!" << endl;
}
};
int main() {
//Only gets outputted, if the lines, that don't work are commented out:
cout << "Hello ";
// Works:
cout << "World!" << endl;
// Doesn't work:
// SayWorld sw;
// Also doesn't work:
// string str("World!");
// cout << str << endl;
return 0;
}
Edit 2:
I narrowed the error to MinGW, as this picture of a Cygwin-Bash-Terminal demonstrates. (The file was not changed beetween the to g++ calls and contains the example above)
Edit 1 (Legacy)
Toolchain-picture:
-picture removed- (don't think it was necessary)
After reinstalling eclipse and MinGW again, step-by-step following this video tutorial:
http://www.youtube.com/watch?v=77xZOT3xer4
and having the same problem afterwards, I stumbled uppon this post in the eclipse forum:
http://www.eclipse.org/forums/index.php/u/104305/
which has brought me to this solution:
Right-click Project -> Properties -> Run/Debug Settings
Choose executable and hit the "New"-button.
Go to the Environment-Tab and create a new variable named PATH with the value: "C:\MinGW\bin".
As I am no expert, I can't explain to you why it works, but it worked for me. If someone knows, how to do this better or wholly avoid this problem, I'd be glad to listen.
This entry was definitively in my Windows-PATH...
PS.: The problem seems also to be known here:
unable to see the output of c++ program in the console

std::endl crashes Windows 8, compiled using MinGW

I have 3 computers, two of which use Windows 8. Using the latest version of MinGW's g++ (4.8.1-4) my hello world program freezes whenever I compile and run on the Windows 8 computers but not in Windows 7.
#include <iostream>
int main()
{
std::cout << "Hello, World!" <<std::endl;
return 0;
}
This compiles just fine in g++ but running a.exe will display "Hello, World!" then a window will pop up and say "a.exe has stopped working, Windows can check online for a solution to the program...." etc.
Has anybody seen this problem.
Also, I tried "std::cout << "Hello, World!\n" << std::flush;" and this has the same problem. It seems that every function that flushes the buffer causes a crash.
Following Eric's advice, I recompiled the program and ran it in gdb and got the following output:
Program received signal SIGILL, Illegal instruction.
0x00405065 in _Jv_RegisterClasses ()
In the second instance, the '\n' should cause an output flush in any case, although in Windows I believe console output is immediate (or perhaps automatic after a short timeout) in any case without an explicit flush.
I suggest the following experiments:
1) See if it is specific to the C++ library by using the C library (in MinGW Microsoft's C runtime is used rather than glibc):
#include <stdio.h>
int main()
{
printf( "Hello, World!\n" ) ;
return 0;
}
2) Eliminate the exit code by:
int main()
{
return 0;
}
3) No newline at all:
#include <iostream>
int main()
{
std::cout << "Hello, World! ;
return 0;
}
4) Try different compiler options such as optimisation levels, or -fno-builtin for example, or as suggested here: -static-libgcc -static-libstdc++ (although I doubt ``-static-libgcc` will itself have any effect since MinGW uses Microsoft's C runtime DLL and the static library is only available with Microsoft's tools).
I had the same issue and found after a long painful search that I had multiple versions of the mingw provided libstdc++-6.dll on my computer. One was part of the mingw installation the others were part of other installation packages (gnuplot and GIMP). As I had gnuplot in my PATH the compiled mingw exe it would use an older, incompatible version of this dll and crash with the described symptoms. I can, therefore, confirm Dietmar Kühl's suspicion. As suggested above linking the library statically obviously helps in this case as the library functions are included in the exe at compile time.

Linker error with Boost Threads in C++ in Xcode

I have been trying to get a simple c++ program running that was given to me as an example of multi-thread programming. I know it runs on another machine with boost installed, but I am trying to run it in Xcode, and this is giving me some problems.
First I tried to run it with my existing version of boost. However I was getting many errors and after doing some research, found that I needed to update my boost version from 1.47 to 1.52 to fix a few known threading bugs contained in 1.47.
That fixed many of my errors, but was still throwing a few. SO I found out that I had to patch 1.52 using the patch found here: https://svn.boost.org/trac/boost/attachment/ticket/7671/libcpp_c11_numeric_limits.patch
After all that, I am still getting a linker error saying "Library not found for -lboost_thread"
Right now in Xcode, I have the following project settings:
Under search paths, I have the header and library search paths set to "usr/local/include/" - - Under linking I have other linker flags set to "-lboost_thread"
I have both a lboost_thread.a and a lboost_thread.dylib located in usr/local/lib/. How do I make xCode find this, as It seems that should fix my problem.
Also if it helps here is the code I am running:
#include <iostream>
#include <boost/thread.hpp>
using namespace std;
void printNum(char c, int num)
{
for(int i = 0; i < num; i++)
{
cout << c << i << endl;
}
}
int main()
{
cout << "Boost threads!" << endl;
boost::thread t(printNum, 'b', 100);
printNum('a', 100);
cout << "Good bye!" << endl;
}
Image of Error:
Image showing the file in it's location:
Image showing my build settings:
I don't know why the linker isn't finding the library, but I can tell you how to circumvent the issue of library search: Add the full path /usr/local/lib/libboost_thread.a to the Other Linker Flags build setting, without -l or -L.
Your screenshot says /usr/local/lib/libboost_thread.a the advice is to use -I/usr/local/lib/libboost_thread.a maybe that is the thing.

Compiling Eigen library with nvcc (CUDA)

I tried to compile following program (main.cu) with the nvcc (CUDA 5.0 RC):
#include <Eigen/Core>
#include <iostream>
int main( int argc, char** argv )
{
std::cout << "Pure CUDA" << std::endl;
}
Unfortunately, I get a bunch of warnings and errors I can only explain using nvcc instead of the Microsoft compile.
Is this assumption right?
Is there any way to compile Eigen with nvcc? (I actually don´t want to transfer Eigen matrices to the GPU, just access their members)?
If it should not work to compile Eigen with nvcc, is there a nice guide/tutorial about clever ways to seperate host and device code?
I am using CUDA 5.0 RC, Visual Studio 2008, Eigen 3.0.5. To compile the .cu file I used both, the rules file included in CUDA, aswell as the custom build step produced by CMake. Using the CUDA rule file, I targeted the build at compute capability 3.0.
Thanks for your advice.
PS: If I compile the same code with the host compiler it works perfectly.
NVCC invokes the normal host compiler but not before it has done some preprocessing, so it's likely that NVCC is struggling to parse the Eigen code correctly (especially if it uses C++11 features, but that's unlikely since you say VS2008 works).
I usually advise separating the device code and wrappers into the .cu files and leaving the rest of your application in normal .c/.cpp files to be handled by the host compiler directly. See this answer for some tips on getting this set up with VS2008.
It looks like one of the core contributors to Eigen is porting it to be compatible with CUDA. The idea would be to call it from kernel code.
Starting with Eigen 3.3, nvcc (7.5) succeeds in passing Eigen code to cl (MSVC 2013) (almost?) correctly. For example, the following code produces only 11 warnings:
#include <Eigen/Core>
#include <iostream>
int main()
{
std::cout << "Hello Eigen\t";
std::cout << EIGEN_WORLD_VERSION << "." << EIGEN_MAJOR_VERSION << "." << EIGEN_MINOR_VERSION << "\n";
std::cout << "Hello nvcc\t";
std::cout << __CUDACC_VER_MAJOR__ << "." << __CUDACC_VER_MINOR__ << "." << __CUDACC_VER_BUILD__ << "\n";
return 0;
}
Output as expected (Eigen 3.3rc1):
Hello Eigen 3.2.94
Hello nvcc 7.5.17
But a long list of warnings (see post history if you actually want to see them).
Update:
Unfortunately, using CUDA 8.0 & VS2015 (with both Eigen 3.2.9 & Eigen 3.3rc1) results in compilation errors again:
"operator=" has already been declared in the current scope eigen\src\Core\Block.h 111
"operator=" has already been declared in the current scope eigen\src\Core\Ref.h 89
So close...
Update 2:
This has been fixed in commit 59db051 and is available by either using the development branch or waiting for v3.3 (or 3.3rc2) to actually come out.