0

Currently I have a C++ project template that contains some pre-installed images, defined as an array of bytes inside the code. I want to reorganize the project by moving images to resources, but the problem is that .rc script only accepts absolute paths to images, thus I don't quite understand how can I properly use them for project template. All I want is to place image files somewhere inside the project directory, and reference them in project .rc script using relative paths for portability. Is it even possible?

VS2019 Community 16.5.4 btw.

UPDATE1: I need to include my resources at compile time, thus dynamic file management is not an option. I have .rc file in a project and some PNG images inside it. They are defined using absolute paths like IDB_PNG1 PNG "D:\\absolute\\path\\to\\file.png". The problem is that I can't use paths relative to the project, like IDB_PNG1 PNG "\\path\\inside\\project\\file.png", getting zero portability of my resources in the template.

UPDATE2: One possible solution I've found is to use CustomParameter element and write paths in .rc file like IDB_PNG1 PNG "$projectpath$\\path\\inside\\project\\file.png", then set ReplaceParameters=True for .rc file in .vstemplate. But there is only $projectname$ default parameter and I'm not even sure I can do it for absolute path.

UPDATE3 (ANSWER): Thanks to this question I've found there is undocumented parameter $solutiondirectory$ (hopefully MS won't drop it from future releases). So I simply write my resources as IDB_PNG1 PNG "$solutiondirectory$\\$projectname$\\path\\inside\\project\\file.png" and then these paths are automatically updated by the Visual Studio when project is created. Problem solved!

2
  • Interestingly, MinGW understands relative paths in .rc. Apr 28, 2020 at 12:16
  • @HolyBlackCat I've suddenly found out that it IS possible to use relative paths for resources. BUT these paths are relative NOT to project directory (where '.vcxproj' is), but to 'resource.rc' file. I had 'resource.rc' file in a separate subdirectory, that's why it didn't work for me.
    – Alex xelA
    May 22, 2020 at 17:12

2 Answers 2

1

Thanks to this question I've found there is undocumented parameter $solutiondirectory$ (hopefully MS won't drop it from future releases). So I simply write my resources as IDB_PNG1 PNG "$solutiondirectory$\\$projectname$\\path\\inside\\project\\file.png" and then these paths are automatically updated by the Visual Studio when project is created. Problem solved!

1
  • I've suddenly found out that it IS possible to use relative paths for resources. BUT these paths are relative NOT to project directory (where '.vcxproj' is), but to 'resource.rc' file. I had 'resource.rc' file in a separate subdirectory, that's why it didn't work for me.
    – Alex xelA
    May 22, 2020 at 17:11
0

Assuming you are using a .rc file in the context of something like an icon image, it is possible. You can just use standard library to link to the image file and then read it byte for byte and load it into the arrays that you were using earlier.

If this is not the right context you could share some code for better understanding.

2
  • I need to include my resources at compile time, thus dynamic file management is not an option. I have .rc file in a project and some PNG images inside it. They are defined using absolute paths like IDB_PNG1 PNG "D:\\absolute\\path\\to\\file.png". The problem is that I can't use paths relative to the project, like IDB_PNG1 PNG "\\path\\inside\\project\\file.png", getting zero portability of my resources in the template. Btw, updated the main post.
    – Alex xelA
    Apr 27, 2020 at 18:47
  • Ahh, I see you need portability of source not portability of the compiled executable. Things such as this are not directly embedded into c++ and it depends which IDE you are using. You can look for how your specific IDE handles files like. The IDE also probably has tools that can be configured in the properties menu. This is the reason things like C make exist, to make projects easily compile-able on many different platforms and environments.
    – Cryptic
    Apr 27, 2020 at 19:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.