I have a small snipped of code, which just produces a function to get the current directory for either Windows or Linux platform:
#include <stdio.h> /* defines FILENAME_MAX */
#include <string>
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
std::string getcwd(){
char mCurrentPath[FILENAME_MAX];
GetCurrentDir(mCurrentPath, sizeof(mCurrentPath));
return *(new std::string (mCurrentPath));
}
This is all great and working; however, I'd like to make the getcwd() function inside the namespace, fUtils, hence I did this:
#include <stdio.h> /* defines FILENAME_MAX */
#include <string>
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
namespace fUtils{
std::string getcwd(){
char mCurrentPath[FILENAME_MAX];
GetCurrentDir(mCurrentPath, sizeof(mCurrentPath));
return *(new std::string (mCurrentPath));
}
}
But this gives an error in VSCode which says:
no matching function for call to 'getcwd'
What mistake am I making in this? If this isn't how I put the function to the namespace fUtils, then how should I put it into the namespace?
Your fUtils::getcwd() function is attempting to call itself when the GetCurrentDir macro is evaluated (to getcwd), and this results in a function that expects no argument but is being given two arguments.
To resolve this, add the global namespace operator (::) in the definitions for GetCurrentDir, as follows:
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir ::_getcwd
#else
#include <unistd.h>
#define GetCurrentDir ::getcwd
#endif
Then, in your function body, it is clear to the compiler that your aren't looking for a 'recursive' (and invalid) call.
Use of macros in such context is invitation to problems in future.
Just wrap those functions with own API and all problems will be resolved.
Header file:
#include <string>
namepsace fUtils {
std::string getcwd();
}
Then you can have platform specific cpp files, Windows:
namepsace fUtils {
std::string getcwd() {
char mCurrentPath[FILENAME_MAX];
::_getcwd(mCurrentPath, sizeof(mCurrentPath));
return {mCurrentPath};
}
}
Mac version is obvious.
You also can use this macros inside single cpp file and this way contain them there if you do not what to do larger clean up.
Side notes:
there is boost::filesystem which has such api also use of boost::filesystem::path is quite handy
C++17 introduces std::filesystem, but it is not well supported yet (for example MacOS)
if you wrap this functionality in classes you will open your way for better testing (mocks).
Related
When I use CLion on a Mac to compile C++ code for highlight removal in a single image, there is an error:
Please help me fix it.
#ifndef QX_CVPR09_CTBF_BASIC_H
#define QX_CVPR09_CTBF_BASIC_H
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <numeric>
#include <vector>
#include <process.h>
#include <direct.h>
#include <io.h>
#include <time.h>
#include <string>
#include <memory.h>
#include <algorithm>
#include <functional> // For greater<int>()
#include <iostream>
#if _MSC_VER > 1020 // if VC++ version is > 4.2
using namespace std; // std c++ libs implemented in std
#endif
#define QX_DEF_PADDING 10
#define QX_DEF_THRESHOLD_ZERO 1e-6
class qx_timer {public: void start(); float stop(); void time_display(char *disp=""); void fps_display(char *disp=""); private: clock_t m_begin; clock_t m_end;};
It's a part of my code. The full code is too long.
process.h
process.h is a C header file which contains function declarations and
macros used in working with threads and processes. Most C compilers
that target DOS, Windows 3.1x, Win32, OS/2, Novell NetWare or DOS
extenders supply this header and the library functions in their C
library. Neither the header file nor most of the functions are defined
by either the ANSI/ISO C standard or by POSIX.
Depends on which platform you compile and what standard you use. If you are on linux or compile with c99/ansi standard then this header will probably just not be available (which might be your error)
I use Process::Start function to run an .exe from another program.
I 'm thinking storing the .exe in location where main program (vc++) is stored. That way I could via GetCurrentDir get always the location of .exe too and then import location in Process::Start, as a variable. Is this possible?
GetCurrentDir:
#include <stdio.h> /* defines FILENAME_MAX */
#ifdef WINDOWS
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
char cCurrentPath[FILENAME_MAX];
if (!GetCurrentDir(cCurrentPath, sizeof(cCurrentPath)))
{
return errno;
}
Location is stored in cCurrentPath variable. I tried with:
Process::Start("%s\\application.exe", cCurrentPath);
but didn't work out. How can I fix this?
I'm trying to compile shogun toolbox and I'm getting this fault
C:/shogun-3.0.0/shogun-3.0.0/src/shogun/../shogun/mathematics/Math.h: In static
member function 'static int shogun::CMath::is_finite(double)':
C:/shogun-3.0.0/shogun-3.0.0/src/shogun/../shogun/mathematics/Math.h:1255:20: er
ror: 'ifinite' was not declared in this scope
return ifinite(f);
function itself looks like this.
inline static int is_finite(double)
{
#if defined(isfinite) && !defined(SUNOS)
return ifinite(f);
#else
return finite(f);
#endif
}
I believe similar is described here: http://www.alecjacobson.com/weblog/?p=1768, but I'm not sure as I don't include cmath. Any idea what it can be?
Function is isfinite, not ifinite.
You don't include <cmath> but according to Shogun source here, it does include both <cmath> and <math.h> in the wrong order:
#include <shogun/base/SGObject.h>
#include <shogun/lib/common.h>
#include <cmath> <<<<<<
#include <shogun/mathematics/Math.h>
#include <shogun/mathematics/lapack.h>
#include <shogun/io/SGIO.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h> <<<<<<
So you are supposed to use std::isfinite.
I just downloaded shogun-3.0.0 from here, and there is no occurrence of the string “ifinite” anywhere in the source. The definition of is_finite in Math.h is:
/// checks whether a float is finite
inline static int is_finite(double f)
{
#if defined(isfinite) && !defined(SUNOS)
return isfinite(f);
#else
return finite(f);
#endif
}
If the errors and source text you entered into the question are correct, perhaps the sources you have were corrupted. You should download the source and try again.
at the beginning I have:
#include <sstream>
#include <iostream>
#include <stdio.h>
#include <iomanip>
#include <string>
#define _WIN32_WINNT 0x500 //tells that this is win 2000 or higher, without GetConsoleWindow would not work
#include <windows.h>
using namespace std;
int main() {
PCONSOLE_FONT_INFO lpConsoleCurrentFont;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), false, lpConsoleCurrentFont);
return 0;
}
And undocumented function SetConsoleFont works, but GetCurrentConsoleFont fails at compilation saying that it was not declared in this scope.
-- edit: changed to self sustained code.
GetCurrentConsoleFont is exported on NT4+ at least, the MinGW headers must be wrong.
Try adding this code after your #include's:
#ifdef __cplusplus
extern "C" {
#endif
BOOL WINAPI GetCurrentConsoleFont(HANDLE hConsoleOutput,BOOL bMaximumWindow,PCONSOLE_FONT_INFO lpConsoleCurrentFont);
#ifdef __cplusplus
}
#endif
Your code is also wrong, it should be:
CONSOLE_FONT_INFO ConsoleFontInfo;
GetCurrentConsoleFont(GetStdHandle(STD_OUTPUT_HANDLE), false, &ConsoleFontInfo);
(Any time you see PSOMETYPE as a parameter you usually allocate a SOMETYPE struct on the stack and pass a pointer to this struct as the parameter)
Hans comment above is correct. GetCurrentConsoleFont is not defined in wincon.h. Add the following lines to wincon.h to get this functionality:
BOOL WINAPI GetCurrentConsoleFont(HANDLE, BOOL, PCONSOLE_FONT_INFO );
COORD WINAPI GetConsoleFontSize( HANDLE, DWORD );
GetConsoleFontSize was also missing.
I am trying to compile a 8hz mp3 encoder - C code in QT Creator.
In a file l3psy.c that starts like this
#include <stdio.h>
#include "types.h"
#include "error.h"
#include "layer3.h"
#include "l3psy.h"
#include "fft.h"
#include "tables.h"
The build step complains about PI being undeclared here
for(i=0;i<BLKSIZE;i++) window[i] = 0.5*(1-cos(2.0*PI*(i-0.5)/BLKSIZE));
But types.h, which is obviously included, starts like this:
#ifndef TYPES_H
#define TYPES_H
#include <stdio.h>
#include <time.h>
#include "portableio.h"
#ifdef PI
#undef PI
#define PI 3.14159265358979
#endif
#define PI4 .78539816339745
#define PI64 .049087385212
therefore, there is no way for PI to be undeclared.
What can be the problem here?
also, aside from that stopper, I also get complains about "implicit declaration of function abort" and "implicit declaration of function exit" and "incompatible implicit declaration of built-in function 'exit'", but, they are standard functions of c, why would it complain?
For the first problem, about PI, see Pascal Cuoq's comment (that's all).
For the problems with implicit declarations being reported, you haven't included the relevant header(s) for those functions. IIRC exit and abort are declared by <stdlib.h. But check it out.
Cheers & hth.,