getting projectDir as a string or char* in c++ - c++

I saw a couple of questions relevant to this topic, but I didn't found the easy and common way to do this.
My question is: How can I have $(projectDir) or another Macro as a string or char* in my C++ code?
Thanks

If your compiler supports raw strings then you can add MYMACRO=R"($(ProjectDir))"; to preprocessor definitions (screenshot from Visual Studio 2013):
Then you can add the following to your code:
#pragma message("MYMACRO == " MYMACRO) // will print project dir during compilation
std::cout << MYMACRO << std::endl; //will print project dir at run time

MYMACRO=R"($(ProjectDir))"; prefectly works for me.
I was in need of this for a long time and this came as a perfect solution.
My test cases took input file path relative to $(ProjectDir) while running the exe tried to locate the input file path relative to the bin folder. As a result, I could have either the VS project happy or the exe, but not both.
This solution worked perfectly to make both of them happy.
Thanks #mvidelgauz for the valuable info.

Related

Visual Studio (Intellisense) no operator std::ostringstream <<

I am using Visual Studio 16.5.3 with visual micro plugin (Release 20.03.21.2/3) to compile some ESP32 board sketches. I get a tedious error from the IDE, even tho the code compiles and runs fine. The relevant lines are:
std::ostringstream ss;
ss << "LIGHT: ";
I included <iostream>, <string>, <sstream> and <ostream>
The IDE is complaining about missing << operator, with operands of type std::stringstream and const char[8]. The actual message is (sorry for italian, I guess it's clear anyway):
"C++ nessun operatore corrispondente agli operandi i tipi di operando sono: std::ostringstream << const char [8]"
That would sound something like "no operator << for the operands..."
Visual Studio (Intellisense) no operator std::ostringstream <<
First, you should put the below code under function or main body and then import several head file as you showed above.
Like this:
So if you still face the issue and build the project without any errors, I think it is Intellisense's problem. And you can see that it works well in my side. And you can try this:
Suggestion:
1) close VS Instance, delete .vs hidden folder, bin, obj folder and then reopen your project again.
2) disable any unrelated third-party extensions under Extensions-->Manage Extensions or use devenv /safemode to start VS to test whether it is caused by other extensions.
In addition, if this issue still persists, please share the sample code with us so that we can troubleshoot it as soon as possible.
Any feedback will be expected.

LINK : fatal error LNK1181: cannot open input file 'opencv_world341d.lib'

I have found two similar questions this and this .
But they both use opencv, and opencv indeed provide the corresponding lib. I don't use opencv, and my project is very simple, just hello world.I have changed project default configuration like this
except for these configurations, others all take defaults
I just want to test my project configuration,that works find for win32 debug and release. But not work for x64 debug and release, they all tell me LINK : fatal error LNK1104: cannot open file 'opencv_world341d.lib'
I indeed know my project does not use any opencv lib, but why they tell me I need to use opencv_world341d.lib
my code
#include<iostream>
using namespace std;
int main()
{
cout << "hello world" << endl;
}
Thanks for everyone who comments on the question. I have solved the problem, although this problem not relevant to OpenCV to much, but I think the solution to the problem may be helpful to others. When I build project, visual studio 2019 tell me cannot link opencv_world341d.lib, so I go to Project->Properties->Linker->Input->Additional Dependencies , and I found opencv_world341d.lib. So I need to remove it, but it's readonly. From this we know visual studio using settings file (Microsoft.cpp..users.props) to control global settings including Global search path. These files are located at $(USERPROFILE)\appdata\local\microsoft\msbuild\v4.0 directory. Then I reedit Microsoft.cpp.<Platform>.users.props, delete opencv_world341d.lib, reboot visual studio, problem solved.

How to open input file, C++ on visual studio community 2015?

Does anyone know why the file isn't opening? I also tried just putting "infile.txt" and placing it in the folder of the program and also the debug folder but the ways I used to check for open error both triggered meaning that it could not open. I know I can hard code the location but I don't want to.
I heard you should do stringobj.c_str() but I don't know if that's accurate?
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
string fileloc = "infile.txt";
infile.open(fileloc);
if (!infile)
{
cout << "open fail 1" << endl;
}
bool fail = infile.fail();
if (fail)
{
cout << "open fail 2";
}
return 0;
}
Note that the directory structure (at least for VS2013) is
<base>
- Solution Directory
- Debug
- Release
- Project Directory
- Debug
- Release
The program by default runs in the project directory (even though it is built to the solution/debug directory).
If you accepted the default naming convention when starting your project, you should be putting your file in the "Projects\ConsoleApplication1\ConsoleApplication1" directory, not "Projects\ConsoleApplication1"
Check your working directory in Project Settings -> Debugging. Make your file available there.
First, the documentation for the signature of
std::ifstream::open( const char * filename, ios_base::openmode mode=ios_base::in)
does indicate it requires a const char *, exactly what std::string::c_str() provides. However, there is an overload for open which accepts a const str &, which means it works the same way for both on most implementations.
Otherwise, what you're grappling with is known as the current working directory (or cwd). Apparently you're not sure where THAT directory is. It may be different while you run the debugger on Visual Studio than it is when you run your program from the command line, and it may be different in various IDE's.
I'm not sure why you want to ensure your program only opens a file by name in the current directory, and not give the full path, but...
You may want to inquire what the current working directory is, so you can solve the mystery wherever you try this. In my Visual Studio 2015, the directory ends up being the directory ABOVE debug, but that depends entirely on how your project is configured, and we can't see that out here.
So, try:
std::string cwd = getcwd( NULL, 0 );
This requires a header <direct.h> on Windows in Visual Studio, but it will give you the directory you're trying to figure out.
with
string fileloc = "infile.txt";
if you put infile.txt in the same folder of the cpp file, it should be fine.
btw I delete your first line
#include "stdafx.h"
I use cygwin console, may have minor diff
For my issue - i was stuck at loading image by opencv - i was wrong to place directory with jpg in the root of the C++ project
WRONG:
CORRECT:

C++: Why does libtiff break the console-output?

So finally I’m not able to help myself out by researching anymore. Hopefully you can help me.
I recently decided to learn C++ in the context of my bachelor-thesis: My first aim is to read the pixel-values of a tiff-image with the libtiff-library. Yet, every call of a function of the library seems to break parts of my program.
Here’s the simple “HelloWorld”-Program, it works as it should:
#include "tiffio.h"
#include <iostream>
using namespace std;
int main() {
cout << "Hello" << endl;
// TIFF* tif = TIFFOpen("path to .tif", "r");
return 0;
}
When I uncomment the second line in main(), the code still does compile without errors (except the warning that ‘tif’ isn’t used) and I can start the program as usual. Yet nothing gets printed into the console anymore. Not “Hello” nor any errors.
Any suggestions where the error could be? The code should be alright, I guess I messed something up during the setup of the library or so. Here’s what I did:
I managed to set up eclipse (Mars, 32bit) for C++ and MinGW (32bit) on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
I created a new C++-project with the mentioned code and did the following adjustments in the project-properties:
Project->Properties->C/C++ General->Paths and Symbols->Library
Paths-> Added “D:/… /tiff-4.0.4/libtiff/.libs”
Project->Properties->C/C++ Build->MinGW C++
Linker->Miscellaneous->Set Linkerflags to “-static-libgcc
-static-libstdc++”
Project->Properties->C/C++ Build->MinGW C++ Linker->Libraries-> Set
(-L) to “D:/… /tiff-4.0.4/libtiff/.libs” and (-l) to “libtiff”
I know the .tif is a valid file as I implemented parts of my program in C#, using the LibTiff.NET library by BitMiracle.
Thank you in advance!
Edit 1: The same error occures, even if TIFF* tif = TIFFOpen("path to .tif", "r"); is never called but written down in a dead part of the code. Debugging does not work either, the program seems to be terminated (exit value 0) before a single line is executed, without any error-reports.
I had the same issue and managed to get rid of it.
I set up eclipse (Mars) for C++ and MinGW on my 64bit Win7, then downloaded libtiff 4.0.4 and built it following this instruction.
After the build, I got two directories containing files:
include
tiff.h
tiffconf.h
tiffio.h
tiffio.hxx
tiffvers.h
lib
libtiff.a
libtiff.dll.a
libtiff.la
libtiffxx.a
libtiffxx.dll.a
libtiffxx.la
I also linked all include files and only the libtiff.a to my project and that solved it, ie, the other lines are now executed.
I hope, I helped with this post.

How do I get PCRE to work with C++?

This is a newbie question but I hope I can express my question as clearly as possible.
I'm trying to do pattern matching in C++.
I've downloaded the Win32 version of PCRE from here and I've placed the downloaded pcre3.dll and pcreposix3.dll files into the folder of Dev-CPP's lib folder (I'm using Bloodshed Dev-C++ 4.9.9 IDE).
I've also downloaded a pcrecpp.h header file and have it in the same directory I'm writing the following code (not writing actually. I'm coping example code from a PDF tutorial named PCRE- Perl Compatible Regular Express).
But I can't get it to work. The code is as follows:
#include <iostream>
#include <string>
#include <pcrecpp.h>
using namespace std;
int main()
{
int i;
string s;
pcrecpp::RE re("(\\w+):(\\d+)");
if (re.error().length() > 0) {
cout << "PCRE compilation failed with error: " << re.error() << "\n";
}
if (re.PartialMatch("root:1234", &s, &i))
cout << s << " : " << i << "\n";
}
When I compile the code, Dev-C++ gives me a lot of errors including: "`pcrecpp' has not been declared" and "RE" undeclared.
How should I deal with the downloaded files and fix my problem? Or is there something obvious that I'm missing?
If you specify the file for #include with angle brackets (<>), then the compiler will only look for that header in the locations for external libraries, in so far as the compiler is aware of them.
If you instead use quotation marks (""), then the compiler will also look in the locations for the current project, which typically includes the current directory.
The quick fix for your current problem is to use
#include "pcrecpp.h"
The alternative is to tell the compiler where it can find the headers of the PCRE library.
You will have to tell the compiler where it can find the headers of the PCRE library.
How to do this differs from build system to build system, but if you are using an IDE, then there should be an option somewhere to specify the 'Include directories'. This is where you add the directory of the PCRE headers (with full path).
As a side-note: When the compiler gives you a large number of errors and warnings, always start with fixing the first one. I would guess that in this case it was something like "unable to find header: pcrecpp.h".
It is often the case that, if the compiler tries to continue after encountering a problem, more problems are found that are follow-on problems of the first one. When the first problem is fixed, these also magically disappear.
g++ -lpcrecpp ......
you need to add '-lpcrecpp' to g++ command
cout << “PCRE compilation failed with error: “ << re.error() << “\n”;
I just copied your code and tried to compile it. I got the same error as you reported.
The problem is that string you put to cout is not properly started/ended. You should use real " instead of marks which looks like double quotes (") but it is not. If you fix it, your code should compile w/o any error.
You have included
#include <pcrecpp.h>
1st point to check But is file in the inlcude path of your code. Did you download the installable ? Check where it has been installed on your machine.
2nd point is to check do you have the library paths defined, so that they can be resolved during compiling and linking.