2

Working in Windows environment I'm stuck with the following problem: I'd like to tell the cl compiler to compile (without linking) some source file, putting it in the specified directory, as following:

cl /c /FeOBJDIR\ source.cpp

Unfortunately, the /Fe option is ignored when /c is used (as I found in https://learn.microsoft.com/en-us/cpp/build/reference/fe-name-exe-file ).

Then I can only write :

cl /FeOBJDIR\ source.cpp

This actually puts source.exe in OBJDIR, and source.obj in .\

But I need to put .obj files in different directory from the .cpp files.

Do you know how to tell the compiler to put object files in a specified directory?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Andrea Loforte
  • 117
  • 1
  • 10

2 Answers2

2

I found that the proper compiler option to do this job is /Fo (https://learn.microsoft.com/en-us/cpp/build/reference/fo-object-file-name). /Fo Specifies an object (.obj) file name or directory to be used instead of the default. Also it can be used with the /c option, so you can let the compiler know that you want just compilation, (not linking), and which directory to put the .obj files in.

Andrea Loforte
  • 117
  • 1
  • 10
0

After stumbling across this post looking for similar information, although an old post, I thought I would add what I learnt for others looking for similar information.

Given the following directory structure;

C:\Projects
          |
          Gimp
             |
             bin
             build
             src

(A) - In this example, the C:\Projects directory is the root of my projects, this root folder can be located anywhere you choose and have any name you choose. Each folder within this folder can be any project you want, with any folder name, as an example, project_1, project_2, project_3 etc. As shown, you can see that the project folder for this example is called Gimp.

(B) - Within the project folder, Gimp, I created three folders, a bin folder, a build folder and a src folder.

  1. bin : A folder for our .exe
  2. build : A folder for our .obj files
  3. src : A folder for our script files, .c files (.cpp if using C++), .bat files, etc

(C) Within the src folder I have a C source file named main.c, this file is the entry point to my application. This can be named anything you want, but for this example, main.c is what I use. My main.c file is very simple;

main.c

#include <stdio.h>

int main() 
{
    printf_s("******************************************************\n");
    printf_s("** Gimp v0.0.1 Prototype\n");
    printf_s("** All work, no play, makes Johnson Doe a dull boy... \n");
    printf_s("******************************************************\n");
    return 0;
}

(D) Within the src folder I have a batch script file named build.bat, this file is used to build my project. This can be named anything you want, but for this example, build.bat is what I use. My build.bat file is very simple;

build.bat

@echo off

rem Build .exe using cl

cl /W4 /Fe:..\\bin\\gimp.exe /Fo"..\\build\\" main.c

rem Start gimp.exe

cd ..\bin

gimp.exe

cd ..\src 

(E) While inside of the Developer Command Prompt for VS 2022, which is what I'm using for this project, I traverse to the C:\Projects\Gimp\src directory, the home of my build.bat. I then run the command build.bat in the command prompt, which triggers my build.bat script above.

The part you will be most interested in is "cl /W4 /Fe:..\\bin\\gimp.exe /Fo"..\\build\\" main.c".

According to the official documentation, the compiler command-line syntax is;

CL [option...] file... [option | file]... [lib...] [@command-file] [/link link-opt...]

cl     - The Compiler Command-Line executable
/W4    - Sets the output warning level
/Fe    - Renames the executable file
/Fo    - Creates an object file
main.c - My source file/files

As you can see, the /Fe: option which renames the executable file /Fe:..\\bin\\gimp.exe is where I link my .exe file to the bin folder.

The /Fo option which creates the .obj files /Fo"..\\build\\" is where I link my .obj file creation to the build folder.

In closing, we have a Project folder as the root of our projects and we have sub project folders. Within those sub project folders we have created three sub folders, bin, build and src. Using a batch file starting in the src folder we then linked two of the three folders using cl command line option. Hope this helps someone in the future.