I am trying to write a C program that will use fork() and wait() in Visual Studio 2013. I have downloaded and installed Cygwin, but I am unable to configure Visual Studio to use the appropriate header files. I'm fairly new to the IDE and I was wondering if this community could help me figure out where I am making a mistake.
When starting a new project, this is what I do:
Create new Visual C++ Win32 Console Application
Add new main.c source file to the project
Right-click project properties and configure Include Directories to
include the Cygwin directory: C:\cygwin64
I've taken a screenshot of where I am trying to configure the properties in case this is where I am making my mistake:
My code is rather simple as I am just trying to get it to run at this point:
#include <unistd.h>
int main() {
int i;
i = fork();
if (i == 0) {
printf("I am the child.");
}
else {
wait(&i);
}
}
Here's a screenshot of the error message I receive when I try to build my project:
I apologize in advance if this is a silly question, but I do appreciate any help that you can offer. If there is anything that I can do on my end to help troubleshoot, please let me know.
The problem is that your current include path C:\cygwin64\bin is equivalent to unix /bin, which is for binaries (aka executables). To use unix headers, you need to use the equivalent of /usr/include, which, for your system, should be C:\cygwin64\usr\include.
Using fork() within a Win32 Console application is not going to work. It's a Unix system call. Cygwin is an emulation of the Linux environment that runs on top of Win32.
As others have said you need to be using the Cygwin toolchain to do your compile and builds.
There may be third party libraries to allow Visual Studio to cross-compile for Cygwin, but a Win32 console application is definitely not what you want if you re making Unix system calls.
Ok, there are lots of pieces here to do this, and there are also a lot of answers here with incorrect information.
First, as some have said, you need to modify your include path to contain your include folder from Cygwin, from your install that appears to be C:\Cygwin64\usr\include.
Next, there is an FAQ on this very thing on the Cygwin site. How do I use cygwin1.dll with Visual Studio or MinGW?
From the page:
Use the impdef program to generate a .def file for the cygwin1.dll (if
you build the cygwin dll from source, you will already have a def
file)
impdef cygwin1.dll > cygwin1.def
Use the MS VS linker (lib) to generate an import library
lib /def=cygwin1.def /out=cygwin1.lib
Create a file "my_crt0.c" with the following contents
#include <sys/cygwin.h>
#include <stdlib.h>
typedef int (*MainFunc) (int argc, char *argv[], char **env);
void my_crt0 (MainFunc f) {
cygwin_crt0(f);
}
Use gcc in a Cygwin prompt to build my_crt0.c into a DLL (e.g. my_crt0.dll).
Follow steps 1 and 2 to generate .def and .lib files for the DLL.
Download crt0.c from the cygwin website and include it in your
sources. Modify it to call my_crt0() instead of cygwin_crt0().
Build your object files using the MS VC compiler cl.
Link your object files, cygwin1.lib, and my_crt0.lib (or whatever you
called it) into the executable.
Note that if you are using any other Cygwin based libraries that you
will probably need to build them as DLLs using gcc and then generate
import libraries for the MS VC linker.
Related
I have just installed MinGW
This is the code
#include<stdio.h>
int main() {
printf("Hello, world!");
return 0;
}
I am getting this error:
In file included from c:\mingw\include\_mingw.h:73:0,
from c:\mingw\include\stdio.h:56,
from main.c:1: c:\mingw\include\w32api.h:59:23: fatal error: sdkddkver.h: No such file or directory #include <sdkddkver.h>
^ compilation terminated.
This looks like a problem with your mingw install, missing dependencies, or environment. Your C code is 100% correct so it's not really a "C" language question at all.
Typically... the only thing that REQUIRES mingw is compiling pre-existing Linux code for distribution on Windows.
If that's really what you need you could:
Try installing Windows SDK to see if it resolves. The missing header: sdkdkver.h is part of Windows SDK (per a very quick google search).
Uninstall mingw & re-install with different instructions.
I've had decent success with cygwin in the past. Looks like MSYS2 is a viable alternative that's more dedicated to building windows "native" SW.
Could dig through mingw documentation on environment variable setup. Maybe missing something there.
Alternatives if you DON'T need to compile + distribute existing Linux source for Windows:
Target Windows directly. Free versions of Visual Studio support compiling C or C++ code. So if you're writing FOR WINDOWS, this is your best bet.
Could use Windows Subsystem For Linux ... if you're just looking run Linux SW or do Linux SW development on a natively windows machine.
I have a C program written on linux that I would like to be able to run and develop on window. The program has a few external dependencies on posix/linux libraries so I'm guessing I would need to somehow compile those libraries under windows too. I'm quite new to the linux workflow, and no expert in C and it's compiler make up either. I know something like cygwin and/or msys2 and/or mingw-w64 might be what I need but I'm not really sure how to get it working in a way that would make sense for me.
My program looks like this (only relevant parts shown), and currently runs under my ubuntu linux VM:
// Build:
// gcc -o disc-identifier main.c `pkg-config --cflags --libs glib-2.0 libmirage libisofs-1`
//
// Run:
// disc-identifier test_image.nrg
#include <glib.h>
#include <mirage/mirage.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <limits.h>
#include <libisofs.h>
...
int main (int argc, char **argv)
{
...
}
My ideal goal is to be able to have this as a visual studio project that I can add to a solution and to be able to link the required dependencies (and compile) from within visual studio. I then want it to compile into a portable 32-bit x86 application (maybe with a few accompanying dll files), no bigger than a few MB. But from my understanding I would first need windows compatible versions of the used libraries (glib, libmirage, libisofs), which is where I am a bit lost as to how I would go about this.
I'm really not an expert of cross-platform development but I already build a cross-platform game (made with Irrlicht) mostly developped on Linux.
Have you heard or maybe already build your project with CMake ?
If not, I really invite you to check this very powerfull tool that CMake is.
Without entering in the details, CMake is a tool for cross-platform development that take care of many things and deliver to you ready-to-use build solutions, including for examples: Makefiles and Visual Studio solution files.
With little setups, you should be able to generate a .sln file for Visual Studio already ready to build your project for you (or require additional setups such as paths or DLL).
Sorry in advance if I misunderstand you question or if I'm not thorough technically.
Please let me know if you want for me to expand my answer ;)
Yes, you are right - if the libraries are not "header-only" you need a platform specific build. But this is really easy right now. For this purpose I can recommend vcpkg.
It supports msbuild and cmake integration - really nice.
To install for example glib you only need to open a cmd in your vcpkg folder and enter
vcpkg install glib
Visual studio supports both "msbuild" and "cmake" very well. Another option is to add dependencies via "NuGet" directly in Visual Studio.
If you use those tools, you don't need to compile the libraries yourself.
But what you have to check is if all your libraries support Windows. If not you can develop with Visual Studio and need to debug with "Remote Debugging". The easiest way is to use Windows Subsystem For Linux (WSL).
I have been able to write a CMakeLists.txt file that is able to build my C project on Linux, however, I have been having a lot of trouble to build the project on windows. The cmake .. call succeeds, and Visual Studio 2017 project files are generated, but the build then fails siting:
Error LNK1104 cannot open file 'm.lib'. In the CMakeLists.txt file I am using target_link_libraries(MY_EXECUTABLE m) to try and link the math library, which works on linux, but the above error occurs on windows. After some research, it seems to me that math is handled by the mscvr library on windows, not libm as on linux, but I'm not sure how to configure the CMake file so that I can build on both operating system.
Does anyone have an idea on how I could set this up to be able to build in both environments?
Visual Studio does not need or want you to explicitly request linking the math library. You must avoid adding it as a link library when building for Windows. Instead of unconditionally doing target_link_libraries(MY_EXECUTABLE m), then, you might use:
IF (NOT WIN32)
target_link_libraries(MY_EXECUTABLE m)
ENDIF()
I am trying to use regex in a c program. i am using windows 10 and Dev-C++ . whenever i add header file for regex i.e.
#include <regex.h>
it gives me error
[error] regex.h: NO such file or directory.
i couldn't figure out how to download and install regex library for c in dev-c++. compiler: TDM-GCC 4.9.2 64-bit Release. Thanks for your help.
I assume there something wrong with the include path during the compilation process. There is a nice expanation of the compilation process of c/c++ applications over here, in case you're interested.
Basically, when compiling a c/c++ application, your compiler, in a first step, scans your source files and replaces all #include <file.h> with the content of file.h it finds in its search path.
Dev-c++ uses MinGW and a port of the GNU compiler collection (gcc) for the compilation process.
Now what you have to do:
Figure out whether regex.h is included in your MinGW installation (Check /usr/include.)
Adapt the include path in dev-c++
Sadly I don't have a computer running Windows nearby making it hard to help with these two steps. To install regex on MinGW this package seems promising.
I'm trying to link lua statically into my C++ application with VS2012. I downloaded the vs11_lib files off of sourceforge and added linker dependencies for this file, lua52.lib. I'm now getting all sorts of link errors when I try to compile and I'm pretty sure I missed a step. Again, I'm doing this statically since I'd like my application to run stand-alone. Any pointers would be greatly appreciated!
The best way is to build embeddable Lua yourself. Download source files for your desired version, create a static library project in VS2012, copy the source files (*.h and *.c to the VS project, not VS solution) and add all source files to the project, except luac.c and lua.c, which are needed for standalone executable rather than embedded library (and they conflict with each other in one project anyway).
After that compile the release version and you got yourself lua5.x.lib that you can link against. If it's still not working, then the problem might be that you added linker dependencies in the wrong place.
Lua sources can be compiled as C or C++. I figure the lib files you are trying to use are compiled as C and you are including their headers as C++. The outcome would be that the names of the functions are different; ergo, the linking errors.
If you are using a C lib in a C++ file, wrap the lib's header like so:
extern "C" {
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
}
For more detailed instructions using Lua with Visual Studio, see this article.
UPDATE:
As, #lhf says in a comment, the newer distributions of Lua provide a C++ header lua.hpp which does the same thing. It is described for older distributions in PIL.