Translate CL.exe to mingw command - c++

I have this CL.exe command
cl -Fesample.dll -Oi -LD -D NO_TRACE=1 -MT sample.cpp sample_dll.def
and i would like to compile it on Linux with MinGW and this is what i have so far
i586-mingw32msvc-gcc -Fesample.dll -shared -D NO_TRACE=1 sample.cpp sample_dll.def
Tracing down the cl command options
-Oi is optimisation -> not important at this point
-LD is for generating a dll -> replaced with -shared
-D is for setting a constant in the source -> same on both
-MT is for multithreading -> i was not able to find the mingw option for this as it seems it is not fully supported.
Am i wrong?
But it won't compile and i don't really know why.
Errors i got from testing are ether
-Cannot export samplefunction#4: symbol not defined
or when changing the command through guessing
-Error: invalid Suffix
-Warning: ‘somevariable’ initialized and declared ‘extern’
If you can help me, please do so. Thanks.

To compile a shared library from functions in a C++ code file:
g++ -o esample.so -shared -fpic -DNO_TRACE=1 sample.cpp
Multithreaded (-MT) should be the default these days.
The -LD equivalent is -fpic and -shared
Reference: http://www.cprogramming.com/tutorial/shared-libraries-linux-gcc.html

Related

Getting Cplex example to run: Undefined references

I am trying to get the Cplex basic LP example to work. The code can be found here. I am completely new with c++, but hope to be able to get this running.
I am trying to compile it on linux. I am using the following command to run it
g++ -D IL_STD -I /opt/ibm/ILOG/CPLEX_Studio1271/opl/include ilolpex1.cpp
The -D IL_STD was put there to solve an error as found here. The -I ... was put there to specify the location of the header files. I came up with this myself after a lot of trying and googling, so i am in no way sure this is correct.
Anyway, i when i run it i get errors of undefined references:
/tmp/ccl9O1YF.o: In function `populatebyrow(IloModel, IloNumVarArray, IloRangeArray)':
ilolpex1.cpp:(.text+0x18f): undefined reference to `IloNumVar::IloNumVar(IloEnv, double, double, IloNumVar::Type, char const*)'
I did not make any changes in the file, so i assume the only thing which can be wrong is how the files are linked. I have the feeling it probably just is a simple setting, but after hours of looking i still have no idea how to fix it.
The easiest way to compile the ilolpex1.cpp example is to use the Makefile that is included with the installation. For example, you should do the following:
$ cd /opt/ibm/ILOG/CPLEX_Studio1271/cplex/examples/x86-64_linux/static_pic
$ make ilolpex1
This will produce output, like the following:
g++ -O0 -c -m64 -O -fPIC -fno-strict-aliasing -fexceptions -DNDEBUG -DIL_STD -I../../../include -I../../../../concert/include ../../../examples/src/cpp/ilolpex1.cpp -o ilolpex1.o
g++ -O0 -m64 -O -fPIC -fno-strict-aliasing -fexceptions -DNDEBUG -DIL_STD -I../../../include -I../../../../concert/include -L../../../lib/x86-64_linux/static_pic -L../../../../concert/lib/x86-64_linux/static_pic -o ilolpex1 ilolpex1.o -lconcert -lilocplex -lcplex -lm -lpthread
This will tell you everything you'll need to know if you choose to compile your own application by hand in the future. The details about this are described in the documentation (e.g., here).
Obviously, the iloplex1.cpp file is just a demo how to use IloCplex.
What you yet need is IloCplex itself. This should come either as (a) further source file(s) you have to compile with the demo or as a library you link against.
Have a look at your cplex directories, you might find a lib[...].a file somewhere there, possibly in /opt/ibm/ILOG/CPLEX_Studio1271/opl/lib.
You can link against using GCC's (clang's) -l and -L options. Be aware that when using -l, you leave out lib and .a (-l [...] with above (invalid) sample name).

MinGW, how to avoid linking statically full libstdc++

I am using mingw 64 bit with cygwin.
I know that if I compile using
x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp
the output .exe does not run unless the library path to libstdc++ and other libraries is specified in the Path environment variable.
An alternative is to link statically
x86_64-w64-mingw32-g++.exe -std=c++11 hello.cpp -static-libgcc -Wl,-Bstatic -lstdc++ -lpthread
Since I want a single .exe that I can easily copy on different machines, the second solution is better for me. My only problem is that, since I link statically, even for a simple helloworld program, the size of the executable rises to more than 10 Mb. So my question is: is it possible to link statically only the library parts that are actually used by the program?
The binutils linker on Windows, ld, does not support the --gc-sections argument properly, which in combination with compiler flags -ffunction-sections and -fdata-sections, allow the linker to throw away blocks of unused code.
You are straight out of luck. The only thing you can do is the usual: strip the executable (by running the strip command on it after it is linked) and compile your code optimising for size with -Os.
Keeping in mind these options do not work on Windows (which for the purpose of this answer, includes the Cygwin platform), this is generally how you can do this:
g++ -c -Os -ffunction-sections -fdata-sections some_file.cpp -o some_file.o
g++ -c -Os -ffunction-sections -fdata-sections main.cpp -o main.o
g++ -Wl,--gc-sections main.o some_file.p -o my_executable

g++ can't find ncurses.h despite it being installed

I'm trying to experiment around in ncurses for the first time, but I'm having problems compiling my source code. As far as I can tell, ncurses is installed and in the proper directories.
My makefile is super simple:
.cpp :
g++ -Wall -g -o $* $*.cpp -std=c++11 -lncurses
and here's my output when I try to locate ncurses.h
$ locate ncurses.h
/usr/include/ncursesw/ncurses.h
and when I check to see if it's installed
$ dpkg -l | grep ncurses
ii libncurses5:amd64 5.9+20140118-1ubuntu1 amd64 shared libraries for terminal handling
ii libncursesw5:amd64 5.9+20140118-1ubuntu1 amd64 shared libraries for terminal handling (wide character support)
ii libncursesw5-dev:amd64 5.9+20140118-1ubuntu1 amd64 developer's libraries for ncursesw
ii mtr-tiny 0.85-2 amd64 Full screen ncurses traceroute tool
ii ncurses-base 5.9+20140118-1ubuntu1 all basic terminal type definitions
ii ncurses-bin 5.9+20140118-1ubuntu1 amd64 terminal-related programs and man pages
ii ncurses-term 5.9+20140118-1ubuntu1 all additional terminal type definitions
But g++ tells me this when I try to make
bankacct.cpp:18:29: fatal error: ncurses.h: No such file or directory
compilation terminated.
Unfortunately, I've not got root access and I need to be able to compile on this machine. What are my options?
I've tried including <ncursesw/ncurses.h> based on suggestions from other users, but now g++ is giving me this error:
$ make bankacct
g++ -Wall -g -o bankacct bankacct.cpp -std=c++11 -lncurses
/usr/bin/ld: cannot find -lncurses
and if I try removing -lncurses it gives me this:
$ make bankacct
g++ -Wall -g -o bankacct bankacct.cpp -std=c++11
/tmp/cc8rPQfK.o: In function `main':
bankacct.cpp:23: undefined reference to `initscr'
Now I've tried linking the libraries. Here's what I did:
$ locate libncurse
/lib/x86_64-linux-gnu/libncurses.so.5
/lib/x86_64-linux-gnu/libncurses.so.5.9
/lib/x86_64-linux-gnu/libncursesw.so.5
/lib/x86_64-linux-gnu/libncursesw.so.5.9
/usr/lib/x86_64-linux-gnu/libncurses++w.a
/usr/lib/x86_64-linux-gnu/libncursesw.a
/usr/lib/x86_64-linux-gnu/libncursesw.so
/usr/share/doc/libncurses5
/usr/share/doc/libncursesw5
/usr/share/doc/libncursesw5-dev
/var/lib/dpkg/info/libncurses5:amd64.list
/var/lib/dpkg/info/libncurses5:amd64.md5sums
/var/lib/dpkg/info/libncurses5:amd64.postinst
/var/lib/dpkg/info/libncurses5:amd64.postrm
/var/lib/dpkg/info/libncurses5:amd64.shlibs
/var/lib/dpkg/info/libncurses5:amd64.symbols
/var/lib/dpkg/info/libncursesw5-dev:amd64.list
/var/lib/dpkg/info/libncursesw5-dev:amd64.md5sums
/var/lib/dpkg/info/libncursesw5-dev:amd64.postinst
/var/lib/dpkg/info/libncursesw5:amd64.list
/var/lib/dpkg/info/libncursesw5:amd64.md5sums
/var/lib/dpkg/info/libncursesw5:amd64.postinst
/var/lib/dpkg/info/libncursesw5:amd64.postrm
/var/lib/dpkg/info/libncursesw5:amd64.shlibs
/var/lib/dpkg/info/libncursesw5:amd64.symbols
So then I tried two variations of my makefile:
g++ -Wall -g -L/usr/lib/x86_64-linux-gnu/ -o $* $*.cpp -std=c++11 -lncurses
and
g++ -Wall -g -L/lib/x86_64-linux-gnu/ -o $* $*.cpp -std=c++11 -lncurses
which still gave me the errors undefined reference to 'initscr' (without -lncurses) or /usr/bin/ld: cannot find -lncurses (with it)
-lncurses
tells the linker to look for a library called "ncurses.". You clearly indicate that's not what your library is called:
/usr/lib/x86_64-linux-gnu/libncursesw.a
You need
-lncursesw
You don't need to modify the source code to specify <ncursesw/ncurses.h> you can simply add
-I/usr/include/ncursesw

Setting Up CPLEX in Eclipse C++ on Linux

I have installed CPLEX 12.6.3 (CPLEX_Studio_Community1263) and I want to integrate CPLEX in my Eclipse C++ project (on Linux). But I don't know which steps I have to follow to include CPLEX in my project.
Even by following exactly the steps shown at this link, it still not working for me (I can't import cpelx.jar in my project). The path of my cplex.jar is
/opt/ibm/ILOG/CPLEX_Studio_Community1263/cplex/lib/cpelx.jar
When I right-click on my project and go to
Properties --> Settings --> GCC C++ Linker --> Libraries
to add the cplex.jar in my project, it is impossible to add the .jar because I can't select it (it is deselected and impossible to select it).
Can some one explain me how I can include CPLEX in my project?
The link you reference is for setting up a Java program. This will not help you.
Instead, you should try running one of the C++ examples shipped with CPLEX. Try the following (assuming your path is correct from above):
$ cd /opt/ibm/ILOG/CPLEX_Studio_Community1263/cplex/examples/x86-64_linux/static_pic
$ make ilolpex1 2>&1 | tee output.txt
This will save the output in output.txt so that you can look at it later. It should give you an idea of what the required command line arguments are.
For example, on my system (x86-64_linux), I see this in the output:
$ make ilolpex1
g++ -O0 -c -m64 -O -fPIC -fno-strict-aliasing -fexceptions -DNDEBUG -DIL_STD -I../../../include -I../../../../concert/include ../../../examples/src/cpp/ilolpex1.cpp -o ilolpex1.o
g++ -O0 -m64 -O -fPIC -fno-strict-aliasing -fexceptions -DNDEBUG -DIL_STD -I../../../include -I../../../../concert/include -L../../../lib/x86-64_linux/static_pic -L../../../../concert/lib/x86-64_linux/static_pic -o ilolpex1 ilolpex1.o -lconcert -lilocplex -lcplex -lm -lpthread
This tells you everything you need to know to compile and link your program. You'll just need to figure out where to enter this information in Eclipse.

GCC Debug Flags SCons

I've been using qmake up till now and it passed the correct flags for GCC to generate debug information when compiling without me ever having to worry about passing the correct flags. Now I have to use SCons due to the needs of the project and I've not yet still gotten used to it. How do I pass debug flags to GCC using SCons so that GCC can generate debug information for GDB? (By the way, I already tried '-g', but it didn't work.)
Thanks in advance to anyone who replies!
To pass flags to the compiler you can set one of the following SCons Construction Variables.
CPPFLAGS
User-specified C preprocessor options
CFLAGS
General options that are passed to the C compiler (C only; not C++).
CCFLAGS
General options that are passed to the C and C++ compilers
CXXFLAGS
General options that are passed to the C++ compiler
For example, if you are only using C++, you can do the following:
env = Environment()
env.Append(CXXFLAGS='-g')
env.Program(target='yourTarget', source='yourSource.cc')
You can add options to the SCons command line using AddOption. For example, your SConstruct could look like this:
AddOption('--dbg', action='append_const', dest='cflags', const='-g')
AddOption('--opt', action='append_const', dest='cflags', const='-O2')
env = Environment()
env.MergeFlags(GetOption('cflags'))
SConscript('SConscript', exports={'env':env})
Your SConscript wouldn't change:
Import('env')
env.Program('foo.c')
You control the compilation with command-line parameters to scons:
$ scons -Q
gcc -o foo.o -c foo.c
gcc -o foo foo.o
$ scons -Q --dbg
gcc -o foo.o -c -g foo.c
gcc -o foo foo.o
$ scons -Q --opt
gcc -o foo.o -c -O2 foo.c
gcc -o foo foo.o
Reference:
http://www.scons.org/doc/HTML/scons-user/apd.html
https://docs.python.org/2/library/optparse.html