Compiled files

There is a code that I can easily run through the terminal:
g++ encode.cpp huffman.cpp -o main
./main inputFile.txt compressedFile.huf

But how can I compile this in the IDE?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "huffman.hpp"
using namespace std;

int main(int argc, char* argv[]) {
    if (argc != 3) {
        cout << "Failed to detect Files";
        exit(1);
    }
    
    huffman f(argv[1], argv[2]);
    f.compress();
    cout << "Compressed successfully" << endl;
    
    return 0;
}


I tried huffman f("inputFile.txt", "compressedFile.huf"); but it is without results.
> But how can I compile this in the IDE?
The same way, or at least as you've shown it.

It rather depends on what IDE you're using.
The problem is generally figuring out what the "current directory" is within the IDE.
It's typically either where the executable is, or the project root directory.
Some IDE's allow you to set what the directory should be when running the program.

Try this in a new console project.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <unistd.h>  // Or something else for windows users.

using namespace std;

int main(int argc, char *argv[])
{
    char cwd[100];
    getcwd(cwd,sizeof(cwd));
    cout << "Hello world!" << endl;
    cout << "CWD=" << cwd << endl;
    cout << "EXE=" << argv[0] << endl;
    return 0;
}


So what you posted should work, providing the input files are in the directory your IDE assigns when running the program.
C++17 added the <filesystem> library. Using it doesn't require OS specific libraries to get the current working directory, or change to a different directory such as the OS temp dir.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
// https://en.cppreference.com/w/cpp/filesystem/current_path

#include <iostream>
#include <filesystem>

namespace fs = std::filesystem;

int main()
{
   std::cout << "Current working path is: " << fs::current_path() << "\n\n";

   // change the current working path to the temp dir
   fs::current_path(fs::temp_directory_path());

   std::cout << "Current temp path is:\t" << fs::current_path() << '\n';
}

On my Win10 system I get the following output, I can't test for *nix:
Current working path is: "C:\\Programming\\My Projects\\Project1\\Project1"

Current temp path is:   "C:\\Users\\Owner\\AppData\\Local\\Temp"

FYI, the "working directory" is the dir your OS/IDE believes is the directory where the program was launched. When launching within Visual Studio the actual directory where the executable is located can be different.

If I were to run the executable outside of VS I would launch it from one of the sub-folders created during the compile and link process. For x86 (32-bit) non-debug (release) builds the folder(s) would be: "C:\\Programming\\My Projects\\Project1\\Release", x64 (64-bit): "C:\\Programming\\My Projects\\Project1\\x64\\Release".

For debug change "Release" to Debug".

If I copy/move the executable to a different folder the program correctly shows the correct directory where it was launched from.

Long story, short; if C++ has a library to do something you should use the C++ library. Don't use OS specific headers/libraries whenever possible. Better to be OS agnostic if you can be.
Last edited on
Thanks everyone!
Topic archived. No new replies allowed.