I am using dirent.h 1.20 (source) for windows in VC2013.
I can't find mkdir() in it.
How am I supposed to use it? Or can I create a directory somehow only using dirent.h?
simplest way that helped without using any other library is.
#if defined _MSC_VER
#include <direct.h>
#elif defined __GNUC__
#include <sys/types.h>
#include <sys/stat.h>
#endif
void createDir(string dir) {
#if defined _MSC_VER
_mkdir(dir.data());
#elif defined __GNUC__
mkdir(dir.data(), 0777);
#endif
}
Update: Since C++17, <filesystem> is the portable way to go. For earlier compilers, check out Boost.Filesystem.
The header you are linking to is effectively turning your (POSIX) dirent.h calls into (native) Windows calls. But dirent.h is about directory entries, i.e. reading directories, not creating ones.
If you want to create a directory (mkdir()), you need either:
A similar wrapping header turning your (POSIX) mkdir() call into the corresponding (native) Windows function calls (and I cannot point out such a header for you), or
use the Windows API directly, which might be pragmatic but would lead to a really ugly mix of POSIX and Windows functions...
// UGLY - these two don't belong in the same source...
#include <dirent.h>
#include <windows.h>
// ...
CreateDirectory( "D:\\TestDir", NULL );
// ...
Another solution would be to take a look at Cygwin, which provides a POSIX environment running on Windows, including Bash shell, GCC compiler toolchain, and a complete collection of POSIX headers like dirent.h, sys/stat.h, sys/types.h etc., allowing you to use the POSIX API consistently in your programming.
Visual Studio includes the <direct.h> header.
This header declares _mkdir and _wmkdir, which can be used to create a directory, and are part of the C libraries included with Visual Studio.
The other "easy" option would be to use Windows API calls as indicated by DevSolar.
You can use sys/types.h header file and use
mkdir(const char*) method to create a directory
Following is the sample code
#include<stdio.h>
#include<string.h>
#include <unistd.h>
#include<sys/stat.h>
#include<sys/types.h>
int main()
{
if(!mkdir("C:mydir"))
{
printf("File created\n");
}
else
printf("Error\n");
}
mkdir is deprecated. Give #include <direct.h> as a header file. then write
_mkdir("C:/folder")
Related
Just installed C++Builder 10.4 Community Edition. My app is a console multi-threaded app, and uses std::scoped_lock (C++17).
It seems that C++Builder chooses a <mutex> header file that does not define scoped_lock in C:\Program Files (x86)\Embarcadero\Studio\21.0\include\dinkumware64, where the <mutex> header file that is in C:\Program Files (x86)\Embarcadero\Studio\21.0\include\dinkumware64\Dinkum\threads actually does define them, but is not the one used during include resolution.
What am I missing? Has this ever been tested?
Launch C++Builder fresh from install, create a new console, multi-threaded application, take the pre-generated shim code for main() and add this code:
#pragma hdrstop
#pragma argsused
#include <mutex>
#ifdef _WIN32
#include <tchar.h>
#else
typedef char _TCHAR;
#define _tmain main
#endif
#include <stdio.h>
std::mutex m;
int _tmain(int argc, _TCHAR* argv[])
{
std::scoped_lock lock(m);
return 0;
}
And that will fail with an error:
no member named "std::scoped_lock" in namespace "std"
The application is 32 bits, debug. I've tried 64 bits as the <mutex> header is strangely located under dinkumware64/mutex, and debug no/debug, I've tried changing various options but no avail.
Now under dinkumware64/Dinkum/threads/, there is another "mutex" package that includes scoped_lock, but I have no idea why C++Builder selects it or not, and it's not in the std namespace anyway.
The standard library is located in dinkumware64 for 32-bit programs as well, so you should be looking there.
Problem is that scoped_lock is missing from the standard library.
You can easily implement this class by yourself by using std::lock, or just use std::lock_guard if you only have one mutex.
I am currently not sure how I should seperate my code best. I currently programming a software which should run on Linux and Windows. So I decided to put all OS-secificstuff in thier own folder/files.
For example
This is the header file:
#ifdef __linux__
#include <unistd.h>
#elif _WIN64
#include <Windows.h>
#endif
#include <string>
#include <iostream>
#pragma once
class SystemTools
{
public:
// Delay in secounds until the programm continues
static void sleep(int delay);
private:
};
and the OS specific implementation is in the linux/windows folder
Linux:
#ifdef __linux__
#include "../SystemTools.h"
void SystemTools::sleep(int delay)
{
usleep(delay*1000000);
}
#endif
Windows:
#ifdef _WIN64
#include "../SystemTools.h"
void SystemTools::sleep(int delay)
{
Sleep(delay*1000);
}
#endif
This works and I have no problems so far, but when I now have methods which don´t need any OS specific code I created an additional folder "Generic" so I can write the code in there and don´t have to mantain the same code in the linux and windows file. For example like that:
Generic:
#include "../SystemTools.h"
void SystemTools::sleepMin(int delay)
{
sleep(delay*60);
}
#endif
That still workes on Linux but not on Windows (no error but does not compile, used codeblockes for that on windows). So how do I organize my code correct? Should I use only one file with ifdef even it that gets very fast ugly?
(compiler Linux: g++, Windows: should be MinGW)
Firstly I'd suggest you to use the most recent of C++ (C++20 or so) on your project. This way, we can abstract many OS related calls (like threading, synchronization, random numbers and etc).
That means, you won't really need to use too many of OS specific APIs. IE: C++11 and earlier already have a standard way to sleep:
https://en.cppreference.com/w/cpp/thread/sleep_for
In the end, if you really need to call OS specific things on windows and on linux, using a library could be interesting and pay attention that windows C++ compiler (visual studio) really like to use 'pre compiled headers' so, it's interesting to have a single header file where all windows specific headers can be included.
Basically that. You can have a standard Cmake or makefile for your linux build and use .sln Visual Studio project to build it to windows.
That's the way I would do that
I am working on an older project that still has the deprecated "#include iostream.h" inclusions. I understand that iostream.h is deprecated and should not be used, but some of the systems that this code has to run/compile on are old solaris machines running CC and do not have iostream available. My question is: how can I make my more modern g++ compiler accept the iostream.h inclusions.
EDIT: The compilier cannot find the iostream.h file so I am assuming that none of the .h versions of the library are available to g++.
The easiest solution is probably to create a local header file called iostream.h which just includes <iostream> and imports the namespace std. Then, in order for the compiler to allow #include <iostream.h> you add the local path to your include file search path. For g++, this works:
g++ -I local_folder [other flags] …
Incidentally, your remark about
… the deprecated "#include iostream.h"
isn’t quite correct: this isn’t deprecated because it has never been legal C++.
I'd take a step back and write another intermediate header you use everywhere instead that does something like:
#if defined(sun) || defined(__sun)
# if defined(__SVR4) || defined(__svr4__)
/* Solaris */
#include <iostream>
# else
/* SunOS */
#include "iostream.h"
# endif
#else
/* Sane, modern system */
#include <iostream>
#endif
Where or how can I find the correct C headers to include in a C++ program to obtain the declaration of C functions declared in a POSIX compliant environment?
I'm asking this because I needed to use the open() system call in my C++ program for my purposes, so I initially tried to include the headers mentioned in the online documentation about open() (in the SYNOPSIS section), which are sys/stat.h and fcntl.h. However when trying to compile, the compiler complained that open() was not declared. After a search on google, I found that another possibility was unistd.h. I tried using that header and the program compiled. So I went back to the POSIX documentation to read more about unistd.h to check if open() was mentioned there, but I could not find anything about it.
What am I doing wrong? Why is there this discrepancy between the POSIX documentation and my GCC environment?
On my Linux Debian/Sid, the man 2 open page states:
SYNOPSIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
So you need to include all three above files. And open is declared in /usr/include/fcntl.h but needs declaration from the other two includes.
And the following test file
/* file testopen.c */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int
testopen (void)
{
return open ("/dev/null", O_RDONLY);
}
compiles with gcc -Wall -c testopen.c without any warnings.
So jenerally I have small C++ project based on OpenSource crossplatform libs. So it probably would compile under linux. So I hited the point when I need to implement some defenatly platform specific class functions.
I have a class header with all functions declarations and cpp file with realisations. So first: how to declare my platform specific functions in header so when I'll try to compile under linux it will not try to compile windows specific ones... and when on windows compiler will not try to compile linux functions include headers etc.
So for windows I need some how wrap such super specific functions
HRESULT EnumerateDevices(REFGUID category, IEnumMoniker **ppEnum)
void DisplayDeviceInformation(IEnumMoniker *pEnum)
And some headers
#include <windows.h>
#include <dshow.h>
#pragma comment(lib, "strmiids")
While for linux I have such headers
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/videodev.h>
And I have function with name of void PrintCamerasList() which I wanna have one for bouth platfrms realisations for which I have seprate.
I hope you see what I need. So generally I need some example using my functions or once you can invent - let your imagination flow!)
So why do I need it all - I am creating some console app using OpenCV and I need to list user cameras names. OpenCV cannot do this on its own. so I asked how to do it for bouth platforms of my intrest - windows and linux
You want to look into platform specific macros and surround for example your MSVC specific code with some #ifdef _WIN32 / #endif pairs.
Take a look at http://predef.sourceforge.net/ for an extensive list of pre-defined macros various compilers provide to distinguish between operating systems, compilers, and processor architectures. They will allow you to distinguish between more than just Win32 and Linux if necessary.
Common practice is to use a compiler flag such as
#ifdef WIN_32
// Windows stuff...
#else
// Linux stuff
#endif
Check for the exact values of what windows flag is defined either in your compiler or in the headers you include