diff --git a/Cabal/Distribution/Simple/GHC.hs b/Cabal/Distribution/Simple/GHC.hs
index 965bd8b5b7689fa1d410940b9b4c239634caf2d4..1fad59e6e0e08a694d9475c08345b7d7b7cf3561 100644
--- a/Cabal/Distribution/Simple/GHC.hs
+++ b/Cabal/Distribution/Simple/GHC.hs
@@ -1268,7 +1268,11 @@ gbuild verbosity numJobs pkg_descr lbi bm clbi = do
     runGhcProg compileTHOpts { ghcOptNoLink  = toFlag True
                              , ghcOptNumJobs = numJobs }
 
-  unless (gbuildIsRepl bm) $
+  -- Do not try to build anything if there are no input files.
+  -- This can happen if the cabal file ends up with only cSrcs
+  -- but no Haskell modules.
+  unless ((null inputFiles && null inputModules)
+          || gbuildIsRepl bm) $
     runGhcProg compileOpts { ghcOptNoLink  = toFlag True
                            , ghcOptNumJobs = numJobs }
 
diff --git a/Cabal/changelog b/Cabal/changelog
index d8cc3b7448201faa4f4601b641bd2e5e9791375b..ce3ddbdfef87bcb935b881e3ce5a0d8f8180dd44 100644
--- a/Cabal/changelog
+++ b/Cabal/changelog
@@ -1,6 +1,7 @@
 -*-change-log-*-
 
 2.2.0.0 (current development version)
+	* Cabal does not try to build an empty set of `inputModules` (#4890).
 	* Add `HexFloatLiterals` to `KnownExtension`
 	* Added `virtual-module` field, to allow modules that are not built
 	  but registered (#4875).
diff --git a/cabal-testsuite/PackageTests/COnlyMain/foo.c b/cabal-testsuite/PackageTests/COnlyMain/foo.c
new file mode 100644
index 0000000000000000000000000000000000000000..3e9fb57d6aae3622db62451d36c085abd791de1e
--- /dev/null
+++ b/cabal-testsuite/PackageTests/COnlyMain/foo.c
@@ -0,0 +1,6 @@
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+    printf("Hello world!");
+    return 0;
+}
diff --git a/cabal-testsuite/PackageTests/COnlyMain/my.cabal b/cabal-testsuite/PackageTests/COnlyMain/my.cabal
new file mode 100644
index 0000000000000000000000000000000000000000..534778572c645171ba1dfc845d236b7f9be8c36d
--- /dev/null
+++ b/cabal-testsuite/PackageTests/COnlyMain/my.cabal
@@ -0,0 +1,10 @@
+name:           my
+version:        0.1
+license:        BSD3
+cabal-version:  >= 2.1
+build-type:     Simple
+
+executable foo
+    -- default-language is required by cabal-version >= 1.10
+    default-language: Haskell2010
+    main-is:    foo.c
diff --git a/cabal-testsuite/PackageTests/COnlyMain/setup.cabal.out b/cabal-testsuite/PackageTests/COnlyMain/setup.cabal.out
new file mode 100644
index 0000000000000000000000000000000000000000..0e4443673807443370b94c397759517a7ba55b6d
--- /dev/null
+++ b/cabal-testsuite/PackageTests/COnlyMain/setup.cabal.out
@@ -0,0 +1,6 @@
+# Setup configure
+Resolving dependencies...
+Configuring my-0.1...
+# Setup build
+Preprocessing executable 'foo' for my-0.1..
+Building executable 'foo' for my-0.1..
diff --git a/cabal-testsuite/PackageTests/COnlyMain/setup.out b/cabal-testsuite/PackageTests/COnlyMain/setup.out
new file mode 100644
index 0000000000000000000000000000000000000000..d3dabcf6cd788a768e57f67b98565058ae53fd54
--- /dev/null
+++ b/cabal-testsuite/PackageTests/COnlyMain/setup.out
@@ -0,0 +1,5 @@
+# Setup configure
+Configuring my-0.1...
+# Setup build
+Preprocessing executable 'foo' for my-0.1..
+Building executable 'foo' for my-0.1..
diff --git a/cabal-testsuite/PackageTests/COnlyMain/setup.test.hs b/cabal-testsuite/PackageTests/COnlyMain/setup.test.hs
new file mode 100644
index 0000000000000000000000000000000000000000..af4aa089c58c08b995a3e610950e8f9133538fdc
--- /dev/null
+++ b/cabal-testsuite/PackageTests/COnlyMain/setup.test.hs
@@ -0,0 +1,4 @@
+import Test.Cabal.Prelude
+-- Test building an executable whose main() function is defined in a C
+-- file
+main = setupAndCabalTest $ setup_build []