I am porting code from Solaris to Windows Visual Studios. To make the code compatible with both, I am using statements such as #if defined (Win32) and #ifdef(OSTYPE_solaris). I found where Win32 is defined, but I cannot find where OSTYPE_solaris is defined. I am thinking that I may not find the definition of it because I am looking for it on Visual Studios, and the header that defines OSTYPE_solaris is not available on Visual Studios. Am I correct in that assumption? If so, does anyone know where OSTYPE_solaris is defined? I googled it a bit, but I was not finding luck with this question. I am also having the same conundrum with OSTYPE_linux.
My guess is they are defined by compiler itself. If Solaris is using gcc as compiler, you could check compiler defined variables like this:
echo "" | gcc -E -dM -
But i think you should not ifdef by platform in most cases, it is better to use feature based defines like HAVE_SOCKET_H. But that requires much more work on multiplatform build system providing these defines and you may not need them.
Related
I have exactly the opposite problem to VSCode turn of _WIN32 define - Visual Studio Code is failing to define _WIN32 for me. This is in a cross-platform project that is being developed on Windows with the Microsoft compiler, but needs to also be able to compile on Linux, so I have
#ifdef _WIN32
#include <windows.h>
failing, and VS Code then marks all references to Windows API types etc. with red underlines. (The include mechanism itself is working fine, e.g. it has no problem including regular C++ headers.)
Is there any known reason why VS Code on Windows might fail to define _WIN32? The question I linked suggests it should, and I haven't knowingly changed any settings related to it.
Most likely, the problem is VSCode is using the wrong C++ compiler to gather the predefined macros, and that compiler does not predefine _WIN32.
To check, in Command Palette (Ctrl+Shift+P), run "C/C++: Log Diagnostics". The output will show you which compiler VSCode found and what it detected as its built-in include path and preprocessor defines. If my guess is correct, the diagnostics will show the wrong compiler being used, and _WIN32 missing.
Assuming so, to solve this, use the command Palette to run "C/C++: Edit Configurations (UI)", then set "Compiler path" to point at your compiler executable (cl.exe in this case). That should solve the problem because VSCode will then query that compiler to determine the predefined macros, which will include _WIN32. Re-run the diagnostics to confirm.
(I just gave a similar answer to the question linked to in the question above, as I think both questions have essentially the same problem and solution, just with different details.)
I am trying to make some F# code conditional based on compile-time environment and can't find any OS-specific definitions that F# compiler would recognise. #if MONO doesn't work. Is there any way to detect OS at compile-time for F#?
I don't think there is any out-of-the box way of doing this. You will need to define your own symbol such as MONO and call the compiler with --define:MONO when building the Mono version.
That said, I would be a bit careful about this - there is no clear concept of "compiled for Mono" in F# and .NET. When you compile F# code, you always get a dll or exe that you can run anywhere and nothing prevents you from copying one build output and running it in another environment.
There is a standard way of detecting where the program is running at runtime using:
Type.GetType("Mono.Runtime") != null
You might have good reasons for wanting two separate builds, but it might be worth considering whether you can just make the appropriate choices at runtime (that probably does not work if you rely on some Windows-specific libraries such as WinForms charting).
I understand it is somehow making a connection so that a compiler when envokes connects a source code to whatever libraries that it needs to.
But what is going on a more technical level, or better put what do I need to know in order to confidentally compile code.
I'm working with C++ and MinGW, and have started to look into build files and stuff for Sublime Text 2 (Have learned mostly under unix, or Java + eclipse so far). But what I don't understand what is adding a compiler to your path do for you?
Do I need to add it for every folder I want to compile from? Or is it system wide? I'm really learning this stuff for the first time, we we're never showed how to set up development environments or even deploy code on other systems.
You probably mean include paths and library paths in the compiler:
include paths: where the compiler will look for headers; and
library paths: where the linker, invoked by the compiler, will look for binary libraries to finish building your project.
If that is the case, look here for a gentle explanation.
Basically, what is happening is that the compiler looks in certain places for symbols defined by the operating system and other libraries installed system-wide.
In addition to those paths, you need to tell the compiler where to find the symbols defined in your own project.
You may also mean something related to installing the compiler itself or configuring the editor to use it.
In that case, what is happening is that you need to tell the build system where to find the executable for the compiler.
Basically, what is probably happening is that your editor wants to know where the compiler is so that it can provide real time feedback on your code. Adding the compiler to the system path will usually, but not always, solve your problem.
In more detail:
A C++ build is a rather complex tool chain, involving determining dependencies, preprocessing, compiling, and linking. There are tools that automate that tool chain, and those tools are in turn wrapped into the functionality of modern IDEs like Eclipse, Visual C++, or Sublime Text 2. You many need to tell your editor where to find the tools it uses to provide you with those services.
Is it possible to compile libexif with Visual Studio 2010? I have been trying to do so and have been running into a whole slew of problems. I cannot find any information about whether anybody has successfully done this before. I know I can use MinGW to compile the library, but I am in a situation where I need it to be compiled with Visual Studio and then need to link to it from a Visual C++ app. Is this possible?
To answer your question: Yes it is possible... but it is a bit of a hack. Libexif uses functions that MSVC has chosen not to implement. See my working example VS2010 project below (if you don't like downloading files then skip to my explanation of what needed changing to get it to work below):
https://www.dropbox.com/s/l6wowl8pouux01a/libexif-0.6.21_CompiledInVS2010%2BExample.7z?dl=0
To elaborate, the issues that needed a "hack" (as hinted in the LibExif readme-win32.txt documentation) are:
Libexif uses inline in several places which is not defined in VS for C, only C++ (see this)
Libexif uses snprintf extensively in the code which is not defined in VS (see here)
You need to create the config.h yourself without a ./configure command to help you. You could read through the script but most of it doesn't make sense for Windows VS2010.
You will need to define GETTEXT_PACKAGE because it's probably setup in the configure file. I just choose UTF-8, whether that is correct or not I'm not sure.
There was a random unsigned static * that needed to be moved from a .c file to the .h file as C in VS doesn't allow you to create new variables inside functions in the particular way they were trying to do.
Read the "readme-win32.txt" file. Advice is:
hack yourself a build system somehow. This seems to be the Windows way of doing things.
Don't get your hopes up. The *nix way of doing things is the configuration script that needs to be run first. It auto-generates source files to marry the library to the specific flavor of *nix. The configuration script is almost half a megabyte. Three times as much code as in the actual .c files :) You cannot reasonably get that working without MinGW so you can execute the script. Once you got that done, you've got a better shot at it with a VS solution. As long as it doesn't use too much C99 specific syntax.
I am having some trouble compiling a programm with gcc on windows which was initially developed with Visual Studio. So far I was able to resolve almost all problems like missing header files and such, but now I am stuck at one last thing: gcc fails to link to one of the third party libs my program uses (FlyCapture2.lib). It tells me that it does not find any of the functions/methods there. I already checked if the library is actually on the library path and that sort of things, but it still does not work.
I searched a bit around and learned that it might have something to do with the format of .libs created with the Microsoft compiler. Is there any way to convert such a lib to be compatible with gcc? Anything else I might have missed?
(I already found this similar question, but its solution won't work here)
In this page the author gives several ways to achieve what you want