Skip to content

Cannot create CppWriter #47

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

Open
gasbug opened this issue Mar 30, 2025 · 2 comments
Open

Cannot create CppWriter #47

gasbug opened this issue Mar 30, 2025 · 2 comments

Comments

@gasbug
Copy link

gasbug commented Mar 30, 2025

It seems that there is an error creating CppWriter, e.g. I tred to compile the following code:

#include "cppwriter/cppwriter.h"
int main(){
    cppcodegen::CppWriter writer;
}

And got the following error:

in function `cppcodegen::CppWriter::emitCompound(cppast::CppCompound const&, std::ostream&, cppcodegen::CppIndent, bool) const':
cppwriter.cpp:(.text+0x2ee7): undefined reference to `cppast::IsClassLike(cppast::CppEntity const&)'

Is this an error of the CppWriter, or is it something to do with my environment?
Thanks!

@satya-das
Copy link
Owner

Is this an error of the CppWriter, or is it something to do with my environment?

You need to link with the cppwriter. For example see how the cppparsertest links with cppwriter:

target_link_libraries(cppparsertest
	PRIVATE
		cppparser
		cppwriter
		boost_program_options
)

@gasbug
Copy link
Author

gasbug commented Apr 1, 2025

I think thats what I am doing (I am pretty new to cmake so I am not entirely sure)

#include "cppwriter/cppwriter.h"
int main(){
    cppcodegen::CppWriter writer;
}
add_library(cppparser STATIC IMPORTED)
set_property(TARGET cppparser PROPERTY
	IMPORTED_LOCATION ${CPP_PARSER_SOURCE_DIR}/builds/cppparser/libcppparser.a)
target_include_directories(cppparser INTERFACE ${CPP_PARSER_SOURCE_DIR}/cppparser/include)

add_library(cppast STATIC IMPORTED)
set_property(TARGET cppast PROPERTY
	IMPORTED_LOCATION ${CPP_PARSER_SOURCE_DIR}/builds/cppast/libcppast.a)
target_include_directories(cppast INTERFACE ${CPP_PARSER_SOURCE_DIR}/cppast/include)

add_library(cppwriter STATIC IMPORTED)
set_property(TARGET cppwriter PROPERTY
	IMPORTED_LOCATION ${CPP_PARSER_SOURCE_DIR}/builds/cppwriter/libcppwriter.a)
target_include_directories(cppwriter INTERFACE ${CPP_PARSER_SOURCE_DIR}/cppwriter/include)

add_executable(testing testing.cpp)
target_link_libraries(testing cppparser cppast cppwriter)
#I tried to add PRIVATE but it doesnt work too

In other files I can use cppparser and cppast without any issue.

If I comment line 631-632 in CppParser/cppwriter/src/cppwriter.cpp and rebuild (Even if 1 test failed out of 3 when make && make test)

Image

I can run this code and get the output I want
(does that mean it linked to cppwriter successfully?):

#include "cppparser/cppparser.h"
#include "cppwriter/cppwriter.h"
#include <iostream>
int main(){
    cppparser::CppParser parser;
    const auto ast = parser.parseFile("../testing2.cpp");
    cppcodegen::CppWriter writer;
    writer.emit(*ast,std::cout);
}

./testing
Output:

Image

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants