Author Topic: .c single file compiles only once. any change and subsequent recompilation fails  (Read 3119 times)

Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
Hello all,

Please bear with me - I am facing a really weird issue. I will try to be as exact as possible.

TL;DR - Compilation is successful only once, then every subsequent attempt (with any minor code change) fails with error (see below). Can only recompile if I manually go and delete .exe file (can do it as simple user, no need for elevated cmd). Running codeblocks as admin has same result. Antivirus is disabled. Fresh install CodeBlocks 20.03, MINGW64 bundle.

---

I am using a fresh install of CodeBlocks 20.03 (installed with the bundled MinGW64 compiler). OS is Windows 10 - 64bit.

I have been using Codeblocks without problem for 4-5 years now (version 17.12 - I just uninstalled and installed 20.03 trying to fix my proboem, but as I expected, it didn't work).

Suddenly, today, whenever I create a new file (just a simple File -> New -> Empty file), save it as test.c for example, write a simple hello world c program - when I compile it (or compile/run) everything works fine.

However, when I make any change to the code, for example change the printout to "hello world2", when trying to recompile I get:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
ld.exe||cannot open output file C:\tmp\test.exe Invalid argument|
||error: ld returned 1 exit status|
||=== Build failed: 2 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

Settings -> Compiler -> ToolChain Executables shows correct path.

I googled around extensively for a solution ..

- I have my antivirus disabled, this cannot be an issue.
- I also searched for processes, both with task manager, and various powershell commands/applets that list all processes, sorted by datetime etc, could not really identify a process that 'stays alive' and causing problems as some people seem to suggest.
- I also stumbled accross a similar issue, described here, that talks about enabling an 'application experience' service in windows that could cause issues - no luck there, service is enabled in my system (as it should be)

Now, I finally found kind of a 'solution', or actually more a sign of what the problem is. If I manually go to a command prompt (not even an elevated one) and delete created .exe file, for example test.exe in the example above, then codeblocks can successfully compile (and run) the updated code, again only once though. Recompiling after a change gives the same error, and I have to manually delete new .exe file etc...

I have cleared my PATH environment because I had another version of GCC sitting there and thought this might (somehow?) create an issue..

I have tried to run Codeblocks as administrator just in case .. Exact same thing happens ..

The strange thing is that all this just happened 'randomly' - I mean I didn't change anything in windows or codeblocks .. It just happened today. Could it be a Windows update that messed something up?

Only thing I tried to do that might be relevant (although I cant understand how) is that I tried to setup VS Code to compile/run C programs - it did so successfully, but shows same behaviour - compiles and runs only once, then I have to go and manually delete .exe file in order for it to run again, and in VS code's case, I need an elevated prompt to delete the .exe .. I couldn't bother with that, and turned back to CodeBlocks (which I actually like to be honest) only to find this weird error here too.

Although it seems the vs code setup messed up things, I suspect it might be a coincidence, since I hadn't run CodeBlocks in some time, perhaps a windows update messed up something and I just had not been aware of it .. Maybe I 'm wrong and vs code did mess something up in my system..


Any help would be GREATLY appreciated .. I 've been fighting with this for4-5 hours now and really don't know what to do .. Unfortunately I don't have windows restore points, I would have tried that too.

Again pointing out that uninstall and fresh install of CodeBlocks 20.03 did nothing - it seems to be a weird OS issue? Codeblocks doesn't have the right to overwrite the .exe file created?


I would really appreciate any help and of course I m looking forward to any suggestions in order to try anything ..


thanks - sorry for the length of this message..


PS - I should point out that even successful compilation, still prints:

||=== Build file: "no target" in "no project" (compiler: unknown) ===|
||=== Build finished: 0 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|

So I guess the 'compiler:unknown' is a "standard" message, although a bit weird - but that's not the problem.. Again I verify that settings->compiler->toolchain executables shows right path (and program compiles and runs (i see output) on first time OK, as well as every time I go and delete the .exe file...



I 'll add one more detail I just noticed, although this is way too big already..

If I only compile - then everything works fine. I mean, I can make changes to the code, and simply compile, everything is successful (no errors) and I see updated timestamps on .o object file, .exe and of course .c file. Problem starts happening as soon as I click 'run' even once on codeblocks - first time the program runs, terminal opens, I see output, but then every subsequent attempt to even simply compile (without even any changes) results in the aforementioned error.

« Last Edit: March 12, 2024, 11:33:36 pm by tkogias »

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1591
Have you checked if your program finishes? Open task manager and check if console runner or your program are still alive.

Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
Have you checked if your program finishes? Open task manager and check if console runner or your program are still alive.

Hello, and thanks for your input.

Yes, it does finish. When I run it the first time, I do see cb_console_runner.exe in tasks, when i close the window (click x on console window showing output), it disappears from task manager. I have also checked through PowerShell, sorting all processes by time - again same behaviour, I see the processes and I stop seeing them when I close the window. The program seems to finish, like it always did when I simply closed the terminal window that opens up. Also, codeblocks compile/run/compile&run buttons are disabled when terminal window is still open.

However, upon closing window, codeblocks re-enables buttons, and although I don't see the process anymore in Windows, compile&run shows the error I pointed out in my first post. I have to go and manually delete .exe file in order to be able to compile&run again.

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1591
Have you tried pressing a key to close the console?

The "No project/No target/Unknown compiler" message has no sense, and
Code
ld.exe||cannot open output file C:\tmp\test.exe Invalid argument|
means something is corrupted.

Go to Settings->Compiler->Other settings and select "Full command line" in Compiler logging. Then do a full rebuild and post the result here in code tags (the # above the edit window).

Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
Thanks for your help. I am trying to provide what you requested.

I checked compiler -> settings -> other settings, compiler logging was already in 'full command line'.

I create a new file, test3.c. It contains a simple printout.

I compile the .c file first (simple compile, not build and run). In codeblocks build log i get:


Code
-------------- Build file: "no target" in "no project" (compiler: unknown)---------------

gcc.exe   -c C:\Users\thomas\test3.c -o C:\Users\thomas\test3.o
gcc.exe  -o C:\Users\thomas\test3.exe C:\Users\thomas\test3.o   
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))


Then I do build & run. Program executes succcessfully, a terminal window opens and I see the results of the printf.

Build log in codeblocks shows:


Code
-------------- Build file: "no target" in "no project" (compiler: unknown)---------------

gcc.exe   -c C:\Users\thomas\test3.c -o C:\Users\thomas\test3.o
gcc.exe  -o C:\Users\thomas\test3.exe C:\Users\thomas\test3.o   
Process terminated with status 0 (0 minute(s), 0 second(s))
0 error(s), 0 warning(s) (0 minute(s), 0 second(s))
 
Checking for existence: C:\Users\thomas\test3.exe
Executing: '"C:\CodeBlocks/cb_console_runner.exe" "C:\Users\thomas\test3.exe"' (in 'C:\Users\thomas')
Set variable: PATH=C:\CodeBlocks\MinGW\bin;C:\CodeBlocks\MinGW;C:\Program Files\Common Files\Oracle\Java\javapath;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\Windows\System32;C:\Windows;C:\Windows\System32\wbem;C:\Windows\System32\WindowsPowerShell\v1.0;C:\Windows\System32\OpenSSH;C:\Program Files\PuTTY;C:\Program Files (x86)\Calibre2;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\MinGW64\bin;C:\Program Files\dotnet;C:\Program Files\nodejs;C:\Program Files (x86)\dotnet;C:\Program Files\GitHub CLI;C:\Program Files\Git\cmd;C:\Users\thomas\AppData\Local\Programs\Python\Python311\Scripts;C:\Users\thomas\AppData\Local\Programs\Python\Python311;C:\Users\thomas\AppData\Local\Microsoft\WindowsApps;C:\Program Files\Java\jdk1.8.0_191\bin;C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2020.3.2\bin;\;C:\Users\thomas\AppData\Local\Android\Sdk\platform-tools;C:\Users\thomas\AppData\Roaming\npm;C:\Program Files (x86)\Nmap;c:\Users\thomas\aliases

This is the terminal window:

Code
1111
Process returned 0 (0x0)   execution time : 1.822 s
Press any key to continue.

 (the only code inside the test3.c file is a
Code
printf("1111");


I close terminal window.

If I do another Build&Run (without changing anything in the code), I get the error. Build log shows:


Code
-------------- Build file: "no target" in "no project" (compiler: unknown)---------------

gcc.exe   -c C:\Users\thomas\test3.c -o C:\Users\thomas\test3.o
gcc.exe  -o C:\Users\thomas\test3.exe C:\Users\thomas\test3.o   
C:/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file C:\Users\thomas\test3.exe: Invalid argument
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))



Could it be that PATH shows two different entries for MINGW compiler, one ending at the /bin directory and one in the parent directory, or is that completely irrelevant?


« Last Edit: March 14, 2024, 12:42:57 pm by tkogias »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7607
    • My Best Post
Did you verify in the Windows task manger that test3.exe is not still running?

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
Yes. It's not there (it disappears when I close the terminal window, as expected).

I also downloaded process explorer and tried to search for processes under that name, or any other meaningful name I could think of, cannot find anything .. I also searched on how to list all running processes via powershell and sort them by datetime for example (show most recent on top so i don't get lost), again I can see the process (name, pid, everything) when the terminal window is open, but when i close it there seems to be no process kept running that could somehow explain 'locking' test3.exe ...

« Last Edit: March 14, 2024, 01:36:41 pm by tkogias »

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7607
    • My Best Post
The "No project/No target/Unknown compiler" message has no sense, and

That means the OP is not using an CB project.

Tim S.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Miguel Gimenez

  • Developer
  • Lives here!
  • *****
  • Posts: 1591
Use the built-in Resource Monitor, see the second answer in this post for a guide.

Offline grem

  • Multiple posting newcomer
  • *
  • Posts: 23
Please try to create Project via project wizard initially (menu "File - New - Project") and then add file or files to compile into project. Then try to rebuild it.

Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
OK .. I checked that tool ..

I reboot, clean machine, I create a new file, test.c.

I build and run, program compiles and runs successfully, terminal window is open (I don't close it yet).

I open up built-in Resource Monitor - screenshot here.

I see the process cb_console_runner.exe (pid=15172). Searching through handles, i see multiple entries, all same PID. To be honest, I can't fully understand handles 'do'.. All have same PID so it's sort of different references to the same process in different parts of the OS?

By the way, task manager obviously shows same process (same id of course, although i did not capture it with screenshot) - screenshot here.

Now, I close the terminal window, and as expected the process disappears from task manager, and in Resource Monitor it shows as 'terminated' (screenshot here - also grayed out, disabled) and when I click on the processes window, it kind of 'refreshes' and the process completely disappears from the processes list. Sorting with PIDs, there is no process with pid 15172 anymore. I can't see anything on bottom window with 'handles' either..

Now, I try to build & run again, only to get the same error:


Code
-------------- Build file: "no target" in "no project" (compiler: unknown)---------------

gcc.exe   -c C:\Users\thomas\test.c -o C:\Users\thomas\test.o
gcc.exe  -o C:\Users\thomas\test.exe C:\Users\thomas\test.o   
C:/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file C:\Users\thomas\test.exe: Invalid argument
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))

Interestingly, I try to manually delete .exe file, and I get "action can't be completed because the file is open in another program".

I close CodeBlocks (just in case) - same message, action can't be completed...

In the past, I could delete .exe files manually ...


Also - irrelevant probabaly - but I performed a CHKDSK on windows, it found that it needed to fix some errors, rebooted, performed thorough CHKDSK, it said it fixed stuff, nothing changed ..


I am searching on how to find that 'other program' that holds a lock on the .exe file - but everything points to checking task manager, killing the process etc - but I cant find any process that deals with it once the terminal window is closed. Process Monitor has no handles on previous PID 15172 ... all processes seem legit and irrelevant to that previous executable ...

I 'm lost... any ideas would be greatly appreciated ...

 


Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
Please try to create Project via project wizard initially (menu "File - New - Project") and then add file or files to compile into project. Then try to rebuild it.

Sorry, I was posting a reply and did not see your message in time.

I did try this just now, created a C project, and then ran the main.c file that comes to it. It compiles and runs successfully first time. Second time, I get the same error. See log: (notice the compiler is now reported, as it's a project not a single file, as someone mentioned earlier).


Code
-------------- Build: Debug in test (compiler: GNU GCC Compiler)---------------

gcc.exe -Wall -g  -c C:\tmp\askiseis\test\main.c -o obj\Debug\main.o
gcc.exe  -o bin\Debug\test.exe obj\Debug\main.o   
C:/CodeBlocks/MinGW/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot open output file bin\Debug\test.exe: Invalid argument
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 0 second(s))
2 error(s), 0 warning(s) (0 minute(s), 0 second(s))



PS - Please dont be confused by the 'tmp' directory name, it's the name of a course, it's not a 'temp' directory .. it's just a regular directory that I created.
« Last Edit: March 14, 2024, 11:51:33 pm by tkogias »

Offline tkogias

  • Single posting newcomer
  • *
  • Posts: 7
Searching around, I came across a set of tools by microsoft, called powertoys, one of which is called file locksmith that seems to do exactly what i want - identify which process(es) keep a certain (or more) files locked.

I downloaded powertoys (~250MB) installed it, made sure I checked the 'run in elevated mode', and test.exe gives no damn results - locksmith cannot identify any process locking that file. However, I try to delete the file and get the error 'cannot delete file cause it's in use by another program'. If i reboot, I can delete it.

I give up for now .. can't think of anything else for the time being.

Offline stahta01

  • Lives here!
  • ****
  • Posts: 7607
    • My Best Post
Virus protection software sometimes creates very weird bugs.
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 64 bit.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline Pecan

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 2787
Searching around, I came across a set of tools by microsoft, called powertoys, one of which is called file locksmith that seems to do exactly what i want - identify which process(es) keep a certain (or more) files locked.

I downloaded powertoys (~250MB) installed it, made sure I checked the 'run in elevated mode', and test.exe gives no damn results - locksmith cannot identify any process locking that file. However, I try to delete the file and get the error 'cannot delete file cause it's in use by another program'. If i reboot, I can delete it.

I give up for now .. can't think of anything else for the time being.
I've had good luck with SysInternals  Handles.exe finding locked files.

https://learn.microsoft.com/en-us/sysinternals/

https://learn.microsoft.com/en-us/sysinternals/downloads/handle
« Last Edit: March 15, 2024, 05:40:26 am by Pecan »