zen
zen

Reputation: 11

undefined reference to `WinMain@16', Trying to solve the twosum problem, but having problem running the code in my visual studio code IDE

#include <iostream>
#include <list>
#include <cstring>
#include <vector>
int main(){
   std::vector<int> nums = {3,2,3};
   int target= 6;
   Solution solution;
   std::vector<int> result = solution.twoSum(nums,target);
   // std::cout << result[0];
}

class Solution {
public:

std::vector<int> twoSum(std::vector<int>& nums, int target) {
    std::vector<int> result;
    for (int i=0; i <= nums.size(); i++){
        for (int j=i+1; i <= nums.size(); i++){
            if(nums[i] + nums[j] == target){
                result.push_back(i);
                result.push_back(j);
                return result;
            }
        }
    }
    return {};
}

};

Trying to solve the twosum problem, but having problem running the code in my visual studio code IDE. Why does it give this error:

[Running] cd "c:\projects\c++\" && g++ twoSum.cpp -o twoSum && "c:\projects\c++\"twoSum
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe:
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../libmingw32.a(main.o):
.text.startup+0xc0): undefined reference to `WinMain@16'
collect2.exe: error: ld returned 1 exit status

Upvotes: 1

Views: 97

Answers (2)

Brecht Sanders
Brecht Sanders

Reputation: 7287

If you have main() in your code but the linker can't find WinMain() then you're building the Windows GUI version (linker flag -mwindows) instead of the console version (linker flag -mconsole).

Check if your linker flags contain -mwindows and remove that and/or add the -mconsole linker flag.

Upvotes: 1

bjhend
bjhend

Reputation: 1713

I'm not a Visual Studio expert, but this looks like you've selected the wrong Visual Studio project type. Your program is a simple console application, while a WinMain is required by graphical applications.

Upvotes: 2

Related Questions