Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LNK2019 unresolved external symbol "id2name", the msvc project config need update #2177

Closed
No-47 opened this issue Oct 20, 2023 · 6 comments
Closed
Labels
bug Something is not working as it should build & packaging Build system and packaging related
Milestone

Comments

@No-47
Copy link

No-47 commented Oct 20, 2023

I try to compile the capstone v5.0.1 project on windows using .\msvc\capstone.sln. bug all test units failed.
so many LNK2019 errors, just like: unresolved external symbol "id2name"
I found and fixed the bug.
The cause of the problem is that the static library project (capstone_static) forgot to include the file: Mapping.c. it just included Mapping.h. that's why so many symbols do not defined.
image
so that's the reason why all the test units project report LNK2019.
when you add it, everything is ok.
but still one error, I haven't had time to see why:
image
This seems to be a problem with the project configuration not being updated. This is a piece of cake to repair, so I won’t go into details.

@No-47
Copy link
Author

No-47 commented Oct 20, 2023

the org errors just like:
image
image

@Rot127 Rot127 mentioned this issue Oct 21, 2023
24 tasks
@Rot127 Rot127 added this to the v5.0.2 milestone Mar 20, 2024
@Rot127 Rot127 added bug Something is not working as it should build & packaging Build system and packaging related labels Mar 20, 2024
@g0th1c54e4

This comment was marked as spam.

@Rot127
Copy link
Collaborator

Rot127 commented Jul 22, 2024

@g0th1c54e4 Please add comments in English only.
@No-47 Sorry, it just got to my attention again. Would you mind opening a PR for this? I won't have a Windows machine/VM in a reasonable time ready.

@g0th1c54e4
Copy link

@Rot127

I encountered the same issue as the original poster while using Capstone 5.0.1. The compilation process in Visual Studio went smoothly without any exceptions reported by the compiler. However, the issue arises with the Capstone static library after the compilation. When I link the Capstone static library in my project, the compiler reports two errors: "LNK2001" and "LNK1120". The "LNK2001" error specifically points out that the symbol "id2name" in the Capstone static library cannot be properly resolved.
error

To address this, I went back to the Capstone source code and located the problematic point as indicated by the compiler. This point is in the file X86Mapping.c, line 943.
项目3

const char *X86_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
	return id2name(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
	return NULL;
#endif
}

Since I am not familiar with the entire Capstone source code, I could only take the following approach to mitigate the issue:
I located the code for id2name (lines 55-67 in Mapping.c), copied this code, and then went back to the problematic point. I pasted the copied code above the problematic point and renamed it to "id2name_fix". Finally, I replaced the "id2name" at the problematic point with "id2name_fix".
项目2

const char *id2name_fix(const name_map *map, int max, const unsigned int id);
const char *id2name_fix(const name_map *map, int max, const unsigned int id)
{
	int i;

	for (i = 0; i < max; i++) {
		if (map[i].id == id) {
			return map[i].name;
		}
	}

	// nothing match
	return NULL;
}

const char *X86_group_name(csh handle, unsigned int id)
{
#ifndef CAPSTONE_DIET
	return id2name_fix(group_name_maps, ARR_SIZE(group_name_maps), id);
#else
	return NULL;
#endif
}

This workaround successfully mitigated the issue. Although this solution may not be elegant, it at least stops the compiler from reporting errors.

---------------other info
Capstone Version: 5.0.1 (source from release)
Compiler: msvc (Visual Studio 2022 Ver.17.9.0 64Bit )
System: Windows 11 23H2

@Rot127
Copy link
Collaborator

Rot127 commented Jul 24, 2024

Could you test the build on the current next branch. I remember someone added Mapping.c to the CMakeFiles I think.

@Rot127 Rot127 mentioned this issue Aug 11, 2024
2 tasks
@Rot127
Copy link
Collaborator

Rot127 commented Sep 10, 2024

The msvc files are removed on the next branch and since the normal cmake build is working fine on Windows I close this here.

Just tested the build on the current v5 branch as well:

cmake.exe -B build
cmake.exe --build build
./build/Debug/cstool.exe
....

@Rot127 Rot127 closed this as completed Sep 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something is not working as it should build & packaging Build system and packaging related
Projects
None yet
Development

No branches or pull requests

3 participants