2 Issues with Codelite MinGW G++ - winapi

I'm using the latest version of codelite with the built in mingw 4.8.1 compiler/linker.
The first issue I have, when I create a new g++ project in Codelite with MinGW and then add the -mwindows flag to the linker build settings to utilize (HBRUSH)GetStockObject(BLACK_BRUSH), any cout statement I have in code doesn't print to the console. I need the -mwindows flag for GetStockObject to work, but I have no idea why it would break the cout statement. The code compiles and runs just fine, just nothing gets printed to the console.
The second issue I have is when I try to instantiate a basic class that is in a different project but still under the same workspace. The linker will give out an undefined reference error when I try to instantiate any class in the other project if I just link the .h class file in the project I'm working in. For example:
Framework *test = new Framework();
will give out
E:/CodeLite/ElysiumEngine/Main/main.cpp:9: undefined reference to `Framework::Framework()'
When I include this api.hpp file in the header:
#ifndef FRAMEWORKAPI_HPP
#define FRAMEWORKAPI_HPP
#include "Framework.hpp"
#endif // FRAMEWORKAPI_HPP
but the code links and runs just fine once I do this:
#ifndef FRAMEWORKAPI_HPP
#define FRAMEWORKAPI_HPP
#include "Framework.hpp"
#include "Framework.cpp"
#endif // FRAMEWORKAPI_HPP
I've never had to link the .cpp file in any other compiler or project I'm working on. It doesn't make any sense to me.
Is there anything I can do to solve these two issues?

Just an answer to your 1st question:
The -mwindows flag causes the compiler to pass --subsystem,windows flag to the linker.
The linker flag --subsystem,windows causes your final executable to marked as a non-console (windowed) application and it also sets the default entry point to WinMainCRTStartup.
WinMainCRTStartup does not initialize console I/O and therefore anything printed/read at the console is ignored

Related

How to build tests using boost.test on mingw64?

I'm trying to write some code using the boost libraries on windows 10. To build the application I have chosen mingw64, which I have installed together with MSYS2.
After downloading and installing the boost libraries(1.76), I tried this example code (https://www.boost.org/doc/libs/1_76_0/more/getting_started/windows.html#build-a-simple-program-using-boost), which I built fine using this command:
g++ .\example.cpp -o test.exe -IC:\Users\Benelli\BoostLib\boost_1_76_0\boost_1_76_0
This example works on my system so I assumed that the boost libraries are installed correctly, although I did not build them, but I understood that the boost.test libary can be used as "header only".
I written a simple code following this tutorial:https://www.boost.org/doc/libs/1_76_0/more/getting_started/windows.html#build-a-simple-program-using-boost.
#define BOOST_TEST_MODULE const_string test
#include <boost/test/unit_test.hpp>
This code does not compile and I really do not get why. Is the boost.test library really "header_only"?
The command I used to build it was:
g++ .\boost_test_example.cpp -o boost_test.exe -IC:\Users\Benelli\BoostLib\boost_1_76_0\boost_1_76_0
Which gives this error message:
For the header only:
I think you need to use boost/test/included/unit_test.hpp as per the boost docs at https://www.boost.org/doc/libs/1_69_0/libs/test/doc/html/boost_test/adv_scenarios/single_header_customizations/multiple_translation_units.html
I had a similar winMain error and had to the define for BOOST_TEST_DYN_LINK to the top of the code (when i was linking against the libraries).
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE const_string test
#include <boost/test/unit_test.hpp>
When not using the header only for the undefined references link with the boost test library, eg -LC:/msys64/mingw64/lib -lboost_unit_test_framework-mt.

CUDA error with Boost make_shared.hpp? [duplicate]

I'm trying to integrate CUDA to an existing aplication wich uses boost::spirit.
Isolating the problem, I've found out that the following code does not copile with nvcc:
main.cu:
#include <boost/spirit/include/qi.hpp>
int main(){
exit(0);
}
Compiling with nvcc -o cudaTest main.cu I get a lot of errors that can be seen here.
But if I change the filename to main.cpp, and compile again using nvcc, it works. What is happening here and how can I fix it?
nvcc sometimes has trouble compiling complex template code such as is found in Boost, even if the code is only used in __host__ functions.
When a file's extension is .cpp, nvcc performs no parsing itself and instead forwards the code to the host compiler, which is why you observe different behavior depending on the file extension.
If possible, try to quarantine code which depends on Boost into .cpp files which needn't be parsed by nvcc.
I'd also make sure to try the nvcc which ships with the recent CUDA 4.1. nvcc's template support improves with each release.

MEX compile error: unknown type name 'char16_t'

I cannot compile any MATLAB MEX code due to the following error:
In file included from /Applications/MATLAB_R2013a.app/extern/include/mex.h:58:
In file included from /Applications/MATLAB_R2013a.app/extern/include/matrix.h:294:
/Applications/MATLAB_R2013a.app/extern/include/tmwtypes.h:819:9: error: unknown type name 'char16_t'
typedef char16_t CHAR16_T;
The only thing that has changed on my machine as far as I can remember is that Xcode was updated to version 5.1 (5B130a).
Any fix for the time being to compile MEX code in MATLAB?
[Running on OS 10.9.2 with Apple LLVM version 5.1 (clang-503.0.38) (based on LLVM 3.4svn)]
By default, the upgraded Clang doesn't set char16_t, which is required by MATLAB.
Quick fix
This works for C or C++ code but needs to be done on each mex command line.
>> mex -Dchar16_t=uint16_t ...
Other solutions below put this definition into the mex configuration or enable C++11.
Permanent solution
Options:
Add -std=c++11 to CXXFLAGS in your mex configuration file AND compile .cpp files instead of .c. The mex config file is mexopts.sh (pre-R2014a) or the .xml file indicated by mex -setup (R2014a+). This is what worked for OP, but the next option works too. Be sure to edit the active/installed config, not the system-wide reference. Try the next solution if you can't tell.
Use a #define or typedef to create char16_t before including mex.h (see "other workaround" below).
In some future version of MATLAB, this will have been fixed. Re-run mex -setup to have MATLAB reconfigure it for you and it works. As of R2014a, this doesn't do the trick.
As a last resort, you can always modify the MATLAB installation, hacking MATLAB's tmwtypes.h as Dennis suggests, but I strongly suggest NOT modifying the MATLAB installation.
Note: If you are using C and cannot or don't want to change to C++, follow the solution in this other answer, OR see the alternative workaround below.
The other workaround
If for some reason you are not able to enable the C++11 standard, you can use the preprocessor to define char16_t. Either put #define char16_t uint16_t before #include "mex.h", or set it with the compiler command line:
-Dchar16_t=uint16_t
Alternatively, use a typedef, again before including mex.h:
typedef uint16_t char16_t;
If these solutions don't work, try changing uint16_t to UINT16_T. Further yet, others have reported that simply including uchar.h brings in the type, but others don't have that header.
I experienced the same error, also directly after upgrading to Xcode 5.1.
The relevant lines (818-824) in the file tmwtypes.h, which causes the error, are:
#if defined(__STDC_UTF_16__) || (defined(_HAS_CHAR16_T_LANGUAGE_SUPPORT) && _HAS_CHAR16_T_LANGUAGE_SUPPORT)
typedef char16_t CHAR16_T;
#elif defined(_MSC_VER)
typedef wchar_t CHAR16_T;
#else
typedef UINT16_T CHAR16_T;
#endif
A solution is to simply change the line
typedef char16_t CHAR16_T;
into
typedef UINT16_T CHAR16_T;
A must admit that I don't know if this affects any function or behaviour of mex files but at least I'm able to compile my c files again using mex.
Please see other answers if this method doesn't work.
I upgraded my gcc/g++ compilers using homebrew to version 4.8 --> gcc-4.8 and g++-4.8.
After that I changed the following lines in the mexopts.sh file:
CXXFLAGS="-fno-common -fexceptions -arch $ARCHS -isysroot $MW_SDKROOT -mmacosx-version-min=$MACOSX_DEPLOYMENT_TARGET -std=c++11"
In my mexopts.sh, this is line 150. I only added the -std=c++11 flag which is what I guess chappjc meant.
EDIT: This is covered in the update by chappjc!
I just add my own experiment (C++ only). The
#define char16_t uint16_t
was causing some problem in the other parts of the mex file. In fact, subsequently to my mex file, char16_t was properly defined. By tracking the chain of includes, the proper type char16_t is set in a file named __config :
typedef __char16_t char16_t;
which is also the first file included from <algorithm>. So the hack consists in including algorithm before mex.h.
#include <algorithm>
#include "mex.h"
and the proper settings are performed, still in a multiplatform manner and without changing anything in the build configuration.
Include uchar.h before including mex.h...works fine. Also, the answer above (adding -std=c++11) only works for c++, not c.
#include <uchar.h>
#include "mex.h"
As part of XCode 5.1.1 char16_t is defined in __config, which is called from typeinfo.
You can add
#include <typeinfo>
before
#include "mex.h"
to have char16_t defined.
This post might help: http://www.seaandsailor.com/matlab-xcode6.html
It was easier than I thought. Just replace all 10.x with your OS X version and add -Dchar16_t=UINT16_T to CLIBS in mexopts.sh file.
It worked on OS X 10.9 Mavericks with Xcode 6 installed.

Preprocessors directives have no effect

I am experiencing technical difficulties with preprocessor directives :
#ifdef, #define
I have a program built by a Makefile, and I have 2 options two build it : standalone or embedded.
I did something like :
#ifdef _mdimode_
//code for embedded
#else
//code for standalone (default)
#endif
And in my main file when I compile in embedded purpose I wrote a :
#define _mdimode_
But it seems that g++ does not recognize or understand it. It always go in the else and never compile the code for embedded version.
Infos:
GNU Make 3.82
g++ (GCC) 4.6.1 20110908 (Red Hat 4.6.1-9)
file suffix : .C
This is a bit of a guess without more information. I assume the code is in a different file than the main file. If this is so then the #define doesn't propagate to that part of the project. You would have to set it in the file that contains the code, or in a header that is #included in it.
You can also choose to set the #define in the options of the compilation command:
g++ -c -D_mdimode_ mycode.C

Compile errors with #include <string> in Cocoa App

I am trying to compile a Cocoa app in xcode 4.0 and I'm getting this error...
fatal error: 'string' file not found
...when trying to compile to .pch file on this line:
#include <string>
I have another xcode project that does the same thing, but does not get the error. I have scoured the build settings for some different, but I can't find one. The only difference is that the project that compiles OK was started as a command line project, not a Cocoa project, but the build setting are the same.
The target OS is Mac OS X 10.6
The error happens when compiling the precompiled header and doesn't get to any of the other files. The only framework that the compiling version has is Foundation.framework and the non-compiling one has it as well.
Why is it not finding in one project and not the other? Any advice?
What is the extension of your source files? If it is ".m", try to change it to obj-cpp ".mm", so that Xcode will deduce correct language.
Or just put c++-specific headers inside "#ifdef __cplusplus" block
Update
The guard must exist for each language compiled in the project because this specific include is in the pch. IOW, if it were all c++ and/or objc++ there would be no error. Evidently, there is at least one file that does not recognize C++ (e.g. C or ObjC sources are also compiled in the target). Therefore, you simply guard it like so:
// MONPrefix.pch
#ifdef __cplusplus
#include <string>
#endif
// same for objc, so your C and C++ sources compile with no error:
#ifdef __OBJC__
#include <Foundation/Foundation.h>
#endif
string is a C++ header (for std::string). If you are looking for stuff like strcpy you need to include string.h

Resources